Welcome Guest, Not a member yet? Register   Sign In
TRUE contoller CRUD? Another way to not repeat myself?
#3

[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();


Messages In This Thread
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-15-2010, 11:08 PM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-15-2010, 11:58 PM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-16-2010, 02:54 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-16-2010, 07:26 PM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 12:06 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 02:11 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 04:02 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 04:06 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 10:23 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 10:31 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 10:45 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 10:49 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 10:58 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-17-2010, 11:52 PM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-18-2010, 12:26 AM
TRUE contoller CRUD? Another way to not repeat myself? - by El Forum - 03-18-2010, 01:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB