[eluser]umefarooq[/eluser]
Hi you can use _remap for reducibility here is little example of controller and view which you can use
Code:
controller
class example extends Controller{
// your database columns or fields
$cols = array('username'=>array('rule'=>'trim|required|xss_clean'),'password'=>array('rule'=>'trim|required|xss_clean'));
function example(){
parent::Controller();
}
function _remap($task){
switch($task){
case 'add':
case 'edit':
$this->form();
break;
default:
$this->view();
break;
}
}
function form(){
// to get id set you segment i set 3 for example you have to set it later and default is 0
$id = $this->uri->segment(3,0);
$this->load->library('form_validation');
// it will set rules for your fields
foreach($this->cols as $key => $v){
$this->form_validation->set_rules($key,$key,$v['rule']);
}
if($this->form_validation->run() == false){
$row = $this->db->get_where('yourtable',array('id'=>$id))->row();
//sending row if there is data while edit
$data = $this->create_form($row);
//for hidden filed for your form
$data['id'] = array('id'=>$id)
$this->load->view('form',$data);
}
else{
$this->save();
}
}
function save(){
$data['id'] = $this->input->get_post('id');
foreach($this->cols as $col => $v){
$data[$col] = $this->input->get_post($col);
}
if($data['id']){
$this->db->update('yourtable',$data,array('id'=>$data['id']))
}
else{
$this->db->insert('yourtable',$data);
}
redirect('anywhere');
}
function creat_form($row){
$fields = array();
foreach($this->cols as $col=>$v){
$fields[$col] = array('name'=>$col,'id'=>$col,'value'=>set_value($col,is_object($row)?$row->$col:''));
}
return $fields;
}
}
form view
echo form_open($this->uri->uri_string());
echo form_input($username);
echo form_password($password);
echo from_hidden($id);
echo form_submit();