# A good example of codeigniter MVC?

What is the difference between this two way and Which one is best? What do you suggest?

1.
Use of query (db) directly in CI_Controller. as:

$id =$this->input->post('id');
$query =$this->db->get_where('table1', array('id' => $id))->row();$this->db->get_where('table2', array('idre' => $query->idre));  Or 2. Use of query (db) in CI_Model and call it in CI_Controller. as: CI_Model: function GetWhere($table,$where){ return$this->db->get_where($table,$where);
}


CI_Controller:

$id =$this->input->post('id');
$query =$this->model_name->GetWhere('table1', array('id' => $id))->row();$this->model_name->GetWhere('table2', array('idre' => $query->idre));  UPDATE: 3. I use the following code true in model as mvc? $id = $this->input->post('checked'); foreach ($id as $val) {$query_check = $this->db->get_where(table3, array('id'=>$val));
$row =$query_check->row();
if($query_check->num_rows() > 0 ||$row->id != $this->session->userdata('id')){ foreach ($query_check->result() as $value) {$id_relation = $value->id_relation;$this->db->where('idre', \$idre)->delete(array(table3, 'table4'));
}
}
}


Whether this is a good example of mvc? How can fix 2 example above for good mvc?

-

## 1 Answer

What is the difference between this two way and Which one is best? What do you suggest?

The difference is that you seperate the concerns in the second example. This means that you can change from database type (e.g. a SQL database, a XML file, an array, ect.) without changing to much code. In fact, you only create a new Model and you are ready!

Whether this is a good example of mvc? How can fix 2 example above for good mvc?

I think you mean that you want to know if this is correct MVC and, if not, how to change it. It is better to put all those logic (except the first line) inside a Model and call that specific method inside your controller. I have used CI once, so I can't help you with code examples...

-