php - MySQL get_where 与 Codeigniter 中的 If 条件
问题描述
我的模型中有以下功能可以明智地过滤官员记录。该功能工作正常。
public function getOfficer()
{
$q = $this->db->order_by('last_name','ASC')->get_where('tbl_officer', array('status' => '1', 'usr' =>$this->session->userdata('id_user')));
if ($q->num_rows() > 0) {
return $q->result();
}
return false;
}
此外,我只想过滤会话中用户 = 4 的官员记录,通过 p_code->8,10 和 24
If 函数如下:
if($usr == 4)
{
$this->datatables->where('tbl_officer.p_code', 8)->or_where('tbl_officer.p_code', 10)->or_where('tbl_officer.p_code', 24);
} else {
$this->datatables->where('tbl_officer.usr', $usr);
}
如何将此 If 条件包含到我上面提到的模型函数中?有什么可以改变的?任何人都可以帮忙吗?
解决方案
只需要改变查询写法
public function getOfficer()
{
$usr = $this->session->userdata('id_user');
$userArray = array(8,10,24);
$this->db->select('*');
$this->db->from('tbl_officer');
if($usr == 4){
$this->db->where_in('usr',$userArray );
}else{
$this->db->where('usr',$usr);
}
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q->result();
}
return false;
}
推荐阅读
- c++ - ADL 在 constexpr 函数中不起作用(仅限 clang)
- r - ggplot2中同一分组箱线图上的多个变量*具有不同的选项*?
- c - 如何在运行它的程序仍在运行时清除文件数据的内存
- pandas - 熊猫 - 按出现顺序排列
- php - 指向外部样式表的链接在 php 中未按预期工作包括
- c++ - 如何从 LCP 数组和 Suffix 数组构造 Suffix 树
- python - 加载保存的 cookie 后页面未登录
- wordpress - 在生产服务器上更改 .htaccess 文件时出现错误 500
- javascript - 悬停在子导航可见性上
- python-3.x - 如何使用 Pandas 从一天中删除 15 分钟的数据