首页 > 解决方案 > 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 次。

标签: phpmysqlajaxcodeigniterdatatable

解决方案


您不能在 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);

推荐阅读