我使用服务器端在jqgrid上创建json。
我的问题是当我想在MySQL查询中包含WHERE
时,因为在搜索运算符中使用了WHERE
。
服务器端代码为
<?php
include("dbconfig.php");
$page = $_REQUEST['page'];
$limit = $_REQUEST['rows'];
$sidx = $_REQUEST['sidx'];
$sord = $_REQUEST['sord'];
// if we not pass at first time index use the first column for the index or what you want
if(!$sidx) $sidx =1;
//array to translate the search type
$ops = array(
'eq'=>'=', //equal
'ne'=>'<>',//not equal
'lt'=>'<', //less than
'le'=>'<=',//less than or equal
'gt'=>'>', //greater than
'ge'=>'>=',//greater than or equal
'nc'=>'NOT LIKE' //doesn't contain
);
function getWhereClause($col, $oper, $val){
global $ops;
if($oper == 'bw' || $oper == 'bn') $val .= '%';
if($oper == 'ew' || $oper == 'en' ) $val = '%'.$val;
if($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') $val = '%'.$val.'%';
return " WHERE $col {$ops[$oper]} '$val' ";
}
$where = ""; //if there is no search request sent by jqgrid, $where should be empty
$searchField = isset($_GET['searchField']) ? $_GET['searchField'] : false;
$searchOper = isset($_GET['searchOper']) ? $_GET['searchOper']: false;
$searchString = isset($_GET['searchString']) ? $_GET['searchString'] : false;
if ($_GET['_search'] == 'true') {
$where = getWhereClause($searchField,$searchOper,$searchString);
// var_dump($where);
}
$totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false;
if($totalrows) {
$limit = $totalrows;
}
$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
mysql_select_db($database) or die("Error connecting to db.");
if ($limit<0) $limit = 0;
// calculate the number of rows for the query. We need this for paging the result
$result = mysql_query("SELECT * FROM enemy_coords");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
if( $count >0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;
// the actual query for the grid data
$SQL = "SELECT * FROM enemy_coords ".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$responce->rows[$i]['id']=$row[ID];
$responce->rows[$i]['cell']=array($row[ID],$row[ID],$row[alliance_name],$row[player_name],$row[player_lvl],$row[player_might],$row[city1_coords]);
$i++;
}
echo json_encode($responce);
?>
我的问题是我是否将mysql查询更改为
SELECT * FROM enemy_coords WHERE category <> 'A',
where should i put the "WHERE category = 'A'"?
答案 0 :(得分:0)
这里有很多选择。一种可能是将初始$ where设置为此条件,然后根据您的要求在搜索打开的其余位置添加AND或OR。为了执行此操作,应从getWhereClause函数中删除WHERE运算符:
function getWhereClause($col, $oper, $val){
global $ops;
if($oper == 'bw' || $oper == 'bn') $val .= '%';
if($oper == 'ew' || $oper == 'en' ) $val = '%'.$val;
if($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') $val = '%'.$val.'%';
return " $col {$ops[$oper]} '$val' ";
}
$where = "WHERE category = 'A'";
...
if ($_GET['_search'] == 'true') {
$where = $where. " AND ". getWhereClause($searchField,$searchOper,$searchString);
// var_dump($where);
}
...
// the actual query for the grid data
$SQL = "SELECT * FROM enemy_coords ".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());
当然,应该执行更多的检查条件以使其达到最佳状态,但这只是一个方向。