我一直在努力让搜索工作在jqgrid上。在线示例不提供搜索控制器端实现的控制器示例。在线查看其他随机帖子后,我试图根据自己的情况调整逻辑。在我的网格数据控制器中找到了这个
我可以通过 $ filters 变量获取每个搜索条件。在第一个for循环中,我可以像 $ item ['field']; , $ item ['op']; 和 $ item ['数据一样访问它们']; 。我从服务器上回来的就是 grouptype_id ='23'
我只是不知道如何为我的codeigniter查询动态生成where子句。请指教。
if($this->input->get('_search')=="true"){
$where = "";
$ops = array(
'eq'=>'=',
'ne'=>'<>',
'lt'=>'<',
'le'=>'<=',
'gt'=>'>',
'ge'=>'>=',
'bw'=>'LIKE',
'bn'=>'NOT LIKE',
'in'=>'LIKE',
'ni'=>'NOT LIKE',
'ew'=>'LIKE',
'en'=>'NOT LIKE',
'cn'=>'LIKE',
'nc'=>'NOT LIKE'
);
$filters = json_decode($this->input->get('filters'), true);
foreach ( $filters['rules'] as $item){
$searchField = $item['field'];
$searchOper = $item['op'];
$searchString = $item['data'];
foreach ($ops as $key => $value) {
if ($item['op'] == $key) {
$ops = $value;
}
}
if ($searchOper == 'eq')
$searchString = $searchString;
if ($searchOper == 'bw' || $searchOper == 'bn')
$searchString .= '%';
if ($searchOper == 'ew' || $searchOper == 'en')
$searchString = '%' . $searchString;
if ($searchOper == 'cn' || $searchOper == 'nc' || $searchOper == 'in' || $searchOper == 'ni')
$searchString = '%' . $searchString . '%';
$where = "$searchField $ops '$searchString' ";
}
echo $where;
}//end if part
答案 0 :(得分:0)
为了那些在这里寻找工作实施的人的利益。以下代码适用于每个搜索条件,无论多么复杂。我正在使用Codeigniter,仅供记录。
if($this->input->get('_search')=="true"){
$where = "";
$ops = array(
'eq'=>'=',
'ne'=>'<>',
'lt'=>'<',
'le'=>'<=',
'gt'=>'>',
'ge'=>'>=',
'bw'=>'LIKE',
'bn'=>'NOT LIKE',
'in'=>'LIKE',
'ni'=>'NOT LIKE',
'ew'=>'LIKE',
'en'=>'NOT LIKE',
'cn'=>'LIKE',
'nc'=>'NOT LIKE'
);
$filters = json_decode($this->input->get('filters'), true);
$first = true;
foreach ( $filters['rules'] as $item){
$where .= $first ? " WHERE" : " AND ";
$searchString = $item['data'];
if($item['op'] == 'eq' ) $searchString = "'".$searchString."'";
if($item['op'] == 'bw' || $item['op'] == 'bn') $searchString = "'".$searchString."%'";
if($item['op'] == 'ew' || $item['op'] == 'en' ) $searchString = "'%".$searchString."'";
if($item['op'] == 'cn' || $item['op'] == 'nc' || $item['op'] == 'in' || $item['op'] == 'ni') $searchString = "'%".$searchString."%'";
$where .= " ".$item['field']." ".$ops[$item['op']]." ".$searchString;
if ($first) {
$first = false;
}
}
$sql = "SELECT * FROM applicant".$where;
$query = $this->db->query($sql);
$result = $query->result_array();
$i=0;
foreach ($result as $myrow){
$responce->rows[$i]['id']=$myrow['id'];
$responce->rows[$i]['cell']=array($myrow['id'],$myrow['contingent_id'],$myrow['firstname'],$myrow['lastname'],$myrow['age'],$myrow['grouptype_id'],$myrow['registrationtype_id']);
$i++;
}
}