Friday, December 4, 2015

Codeigniter Standard select query Model



Today our topics is Codeigniter select query model. Here we will discourse on how do we create a select query model for all select for codeigniter. I create a simple select query for select query and i will share with all of you. By the following function you can select conditional or non-conditional query form select table. You also select order, group, like, where in and limit query by this model function.
function get($array)
{
if($array['table']){
if(isset($array['where'])){
$this->db->where($array['where']);
}
if(isset($array['limit'])){
$limit=explode(',', $array['limit']);
if(isset($limit[1])){
$this->db->limit($limit[0],$limit[1]); }else{
$this->db->limit($array['limit']);
}
}
if(isset($array['order'])){
$this->db->order_by($array['order']);
}
if(isset($array['group'])){
$this->db->group_by($array['group']);
}
if(isset($array['like'])){
$this->db->like($array['like']);
// $this->db->like('title', 'match', 'before');
}
if(isset($array['in'])){
if(count($array['in'])==2){
$this->db->where_in($array['in'][0],$array['in'][1]);
}
} $query=$this->db->get($array['table']);
return $query->result(); }else{
return false;
}
}
Load the model in your controller class. Then call the function using following procedure
$query=array(
'table'=>'table name',//required
'limit'=>'limit offset',//optional
'order'=>'field_name DESC',//optional
'where'=>array('field_name'=>'condition'),//optional
'group'=>'field_name',//optional
'like'=>array('field_name','field_value')//optional
'in'=>array('field_name','field_value,field_value,field_value')//optional
);
$data['products']=$this->data_model->get($query);

For any question on this topics please text on comment box. 

2 comments :

  1. Mahbub,
    You have made a nice method. Complete solution for simple query in a single method.

    ReplyDelete

 

Copyright @ 2014 Tech Tutorial .