Codeigniter搜索Where条件问题



我是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';
// Load the list page view
//$this->load->view('templates/header', $data);
$this->load->view('members/index', $data);
//$this->load->view('templates/footer');
}

我的型号在下面

function getRows($params = array()){
$this->db->select('*');
$this->db->from($this->table);
//$where = "is_verified = 1";
//$this->db->where($where);
if(array_key_exists("conditions", $params)){
foreach($params['conditions'] as $key => $val){
$this->db->where($key, $val);
}
}
if(!empty($params['searchKeyword'])){
$search = $params['searchKeyword'];
$likeArr = array('fname' => $search, 'lname' => $search, 'gran_id' => $search);
$this->db->or_like($likeArr);
}
if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
$result = $this->db->count_all_results();
}else{
if(array_key_exists("user_id", $params)){
$this->db->where('user_id', $params['user_id']);
$query = $this->db->get();
$result = $query->row_array();
}else{
$this->db->order_by('fname', 'asc');
if(array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit'],$params['start']);
}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit']);
}
//$query = $this->db->get();
$query = $this->db->where('is_verified', '1')->get();
$result = ($query->num_rows() > 0)?$query->result_array():FALSE;
}
}
// Return fetched data
return $result;
}

我只想提取所在的记录

is_verified=1

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

谢谢!

在Codeigniter中使用or_like时会发生这种情况,您应该使用group_startgroup_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);
}         

相关内容

  • 没有找到相关文章

最新更新