亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有 Where 條件問題的 Codeigniter 搜索

帶有 Where 條件問題的 Codeigniter 搜索

PHP
長風秋雁 2022-10-09 17:33:56
我是 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 有另一個值。我不知道我應該把這個放在哪里條件,所以當我搜索時,它只能顯示所需的記錄。謝謝!
查看完整描述

2 回答

?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

在 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();

}


查看完整回答
反對 回復 2022-10-09
?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

執行搜索時,您沒有過濾查詢,您只是在搜索之外進行。


嘗試:找到這個塊


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);

}         


查看完整回答
反對 回復 2022-10-09
  • 2 回答
  • 0 關注
  • 140 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號