php - 在codeigniter的自定义函数中使用不等于
问题描述
我制作了一个名为__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();
}
我需要在哪里获取数据field is not equal to 1
。
所以我把这个函数称为:
$this->Usermodel->__getwheredata('users','*',array('rights!='=>1),'id','desc',NULL);
但这里array('rights!='=>1)
将作为where
条件值的数组传递。
那么我该如何解决这个问题呢?
解决方案
也许您需要像这样对整个数组进行字符串化:
$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();
推荐阅读
- python - python使用urllib2和urllib获取请求问题
- c++ - 我创建了一个库,想知道设计是否效率低下
- javascript - 如何在按钮点击反应中添加延迟?
- python - 如何在python中根据索引绘制图形
- javascript - Node.js Telegram 机器人无法从超级组中踢出用户
- sql - 当字符串长度为动态时替换字符串文本的特定部分
- vb.net - 如何处理“ManagementException 未被用户代码处理”C#/Vb.net 中的访问被拒绝
- scala - ZIO Schedule 简单示例
- reactjs - 将复杂的字符串传递给 React 对象
- python - Python将字符串与int列表连接起来