Skip to content
Advertisement

use not equal to in custom function in codeigniter

I have made a function named __getwheredata in MY_Controller:

public function __getwheredata($tablename,$tablefield=array(),$where=array(),$orderbyfield = 'id',$ascdesc = 'desc',$limit = 200,$offset='')
{
    if(is_array($tablefield)){$tablefield = implode(',',$tablefield);}
    print_r($where);exit;
    $data = $this->db
                ->select($tablefield)
                ->from($tablename)
                ->where($where)
                ->order_by($orderbyfield, $ascdesc)
                ->limit($limit,$offset)
                ->get();
    return $data->result();
}

I need to get data where field is not equal to 1.

so I call the function like:

$this->Usermodel->__getwheredata('users','*',array('rights!='=>1),'id','desc',NULL);

but here array('rights!='=>1) will pass as array in value of the where condition.

so how can I resolve this issue ?

Advertisement

Answer

Maybe you need to stringify the whole array like this:

$temp = '';
$index = 1;
$count = count($where);
foreach($where as $key => $value)
{
    $temp .= $key.$value;
    if($index < $count) $temp .= ',';
    $index++;
}
$where = $temp;

Now it will generate where statements as a string like this rights!=1


Edit: or you can do it like this:

foreach($where as $key => $value)
{
    $this->db->where($key, $value);
}

Now your function would look like this:

foreach($where as $key => $value)
{
    $this->db->where($key, $value);
}
$data = $this->db->select($tablefield)
             ->from($tablename)
             ->order_by($orderbyfield, $ascdesc)
             ->limit($limit,$offset)
             ->get(); 
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement