首页 > 解决方案 > 在codeigniter的自定义函数中使用不等于

问题描述

我制作了一个名为__getwheredatain的函数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();
}

我需要在哪里获取数据field is not equal to 1

所以我把这个函数称为:

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

但这里array('rights!='=>1)将作为where条件值的数组传递。

那么我该如何解决这个问题呢?

标签: phpcodeignitercodeigniter-3

解决方案


也许您需要像这样对整个数组进行字符串化:

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

现在它将生成 where 语句作为这样的字符串rights!=1


编辑:或者你可以这样做:

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

现在你的函数看起来像这样:

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

推荐阅读