【问题标题】:use not equal to in custom function in codeigniter在codeigniter的自定义函数中使用不等于
【发布时间】:2018-12-26 11:56:35
【问题描述】:

我在MY_Controller中做了一个名为__getwheredata的函数:

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 条件值的数组传递。

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

【问题讨论】:

    标签: php codeigniter codeigniter-3


    【解决方案1】:

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

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

    【讨论】:

    • 我不想要字符串不等于要求像where('field !=' , 1);
    • 您可以通过向where($string, NULL, FALSE); 添加第三个参数来传递字符串和转义值,但如果您需要遵守规则,我会更新我的答案以匹配它。
    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多