带有 Where 条件问题的 Codeigniter 搜索

我是 PHP 新手,正在学习 Codeigniter。我在搜索查询中放置 where 条件时遇到问题。我的控制器如下所示


public function index(){

    $data = array();


    // Get messages from the session

    if($this->session->userdata('success_msg')){

        $data['success_msg'] = $this->session->userdata('success_msg');

        $this->session->unset_userdata('success_msg');

    }

    if($this->session->userdata('error_msg')){

        $data['error_msg'] = $this->session->userdata('error_msg');

        $this->session->unset_userdata('error_msg');

    }


    // If search request submitted

    if($this->input->post('submitSearch')){

        $inputKeywords = $this->input->post('searchKeyword');

        $searchKeyword = strip_tags($inputKeywords);

        if(!empty($searchKeyword)){

            $this->session->set_userdata('searchKeyword',$searchKeyword);

        }else{

            $this->session->unset_userdata('searchKeyword');

        }

    }elseif($this->input->post('submitSearchReset')){

        $this->session->unset_userdata('searchKeyword');

    }

    $data['searchKeyword'] = $this->session->userdata('searchKeyword');


    // Get rows count

    $conditions['searchKeyword'] = $data['searchKeyword'];

    $conditions['returnType']    = 'count';

    $rowsCount = $this->member->getRows($conditions);


    // Pagination config

    $config['base_url']    = base_url().'members/index/';

    $config['uri_segment'] = 3;

    $config['total_rows']  = $rowsCount;

    $config['per_page']    = $this->perPage;


    // Initialize pagination library

    $this->pagination->initialize($config);


    // Define offset

    $page = $this->uri->segment(3);

    $offset = !$page?0:$page;


    // Get rows

    $conditions['returnType'] = '';

    $conditions['start'] = $offset;

    $conditions['limit'] = $this->perPage;

    $data['members'] = $this->member->getRows($conditions);

    $data['title'] = 'Members List';



}

我只想获取记录

is_verified = 1

所以我把它和我的查询放在一起,它在没有搜索查询的情况下工作正常。如果我搜索它的显示记录,即使_verified 有另一个值。我不知道我应该把这个放在哪里条件,所以当我搜索时,它只能显示所需的记录。

谢谢!


长风秋雁
浏览 113回答 2
2回答

慕森卡

在 Codeigniter 中使用时会发生这种情况,or_like您应该使用group_start和group_end功能将搜索与 where 条件分开if(!empty($params['searchKeyword'])){    $search = $params['searchKeyword'];    $likeArr = array('fname' => $search, 'lname' => $search, 'gran_id' => $search);    $this->db->group_start(); // this will make brackets for your search query    $this->db->or_like($likeArr);    $this->db->group_end();}

泛舟湖上清波郎朗

执行搜索时,您没有过滤查询,您只是在搜索之外进行。尝试:找到这个块if(!empty($params['searchKeyword'])){     $search = $params['searchKeyword'];     $likeArr = array('fname' => $search,'lname' => $search, 'gran_id' => $search);     $query = $this->db->where('is_verified', '1');     $this->db->or_like($likeArr);}         
打开App,查看更多内容
随时随地看视频慕课网APP