php - where 和 or_where codeigniter ajax 数据表搜索不起作用(2 where)
问题描述
如果我使用单个 where,搜索很好,但多个 where 在代码点火器的 ajax 数据表中不起作用。正如您在注释的代码中看到的那样,我使用了不同类型的 where,除了单个 where 之外没有任何作用。
public function get_data1($status, $status1, $id = 0)
{
$column_order = array('s.student_name', 's.student_national_id', 'r.registration_test', 'r.registration_fee', 'r.registration_status');
$column_search = array('s.student_name', 's.student_national_id', 'r.registration_test', 'r.registration_fee', 'r.registration_status');
$order = array('s.id' => 'desc');
$this->db->select('*,count(e.id) as email_count');
$this->db->from('student s');
$this->db->join('email e', 's.id = e.student_id', 'left');
$this->db->join('registration r', 'r.student_id = s.id', 'left');
$this->db->join('classes cl', 'r.class_id = cl.id', 'left');
$this->db->join('student_contact sc', 'sc.student_id = s.id', 'left');
$this->db->join('contact c', 'c.id = sc.contact_id', 'inner');
$this->db->join('address a', 'a.contact_id = c.id', 'left');
$this->db->join('branch b', 'b.id = r.branch_id', 'left');
if($status != '')
{
// $query="SELECT * FROM `registration` where `registration_status` = 'registered' OR `registration_status` = 'payment pending'";
// $this->db->query($query);
// $where = "r.registration_status='registered' OR r.registration_status='payment pending'";
// $this->db->where($where);
// $where = array('r.registration_status' => $status);
// $where1 = array('r.registration_status' => $status1);
// $this->db->where($where);
// $this->db->or_where($where1);
// $this->db->where('r.registration_status'= $status OR 'r.registration_status'= $status1);
// $where = "r.registration_status='registered' OR r.registration_status='payment pending'";
// $this->db->where($where);
// $this->db->or_where_in('r.registration_status',$status1);
$this->db->where('r.registration_status', $status);
// $this->db->or_where('r.registration_status', $status1);
}
$this->db->group_by('s.id');
//$this->db->order_by('r.registration_date', 'desc');
$i = 0;
if($id==0)
{
foreach ($column_search as $key=>$item) // loop column
{
if($_POST['search']['value']) // if datatable send POST for search
{
$search_value = $_POST['search']['value'];
$search_text = $search_value;
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $search_text);
}
else
{
$this->db->or_like($item, $search_text);
}
if(count($column_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
else
if($_POST['columns']) // if datatable send POST for column search
{
$search_value = $_POST['columns'][$key]['search']['value'];
$search_text = $search_value;
if($search_text!='')
{
$this->db->where($item, $search_text);
}
}
$i++;
}
if(isset($_POST['order'])) // here order processing
{
$this->db->order_by($column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($order))
{
$order = $order;
$this->db->order_by(key($order), $order[key($order)]);
}
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result_array();
}
}
我已经尝试了各种位置,但只有在只有 1 个需要从 3 种类型的数据中获取 2 种类型的数据时,搜索才有效。所以我用 where 2 次。
解决方案
您不能在 where 子句中使用数组。
请试试这个:
前任。
$this->db->where('r.registration_status', 'registered');
$this->db->or_where('r.registration_status', 'payment pending');
或者
前任。
$types = array('registered', 'payment pending');
$this->db->where_in('r.registration_status', $types);
推荐阅读
- mapbox - Mapbox - 通过 GL JS vs Mapbox studio 添加源代码和图层
- ios - 在表格视图控制器中添加容器视图时,如何将其移动到最底部?(迅速)
- sql - 使用一个嵌套查询从历史表中查找所有最幸运的玩家
- javascript - 如何使用 Typescript 键入 __proto__ 上的函数
- javascript - js回文任务的递归
- python - Selenium(PYTHON) 语法难题
- java - 没有从字符串值反序列化的字符串参数构造函数/工厂方法->java.util.ArrayList[0]
- excel-formula - 条件格式问题
- swiftui - Swiftui,按钮网格视图
- javascript - 如何正确对齐元素>