0
我有一個簡單的jQgrid,帶有用於添加,編輯,刪除和搜索的工具欄(navGrid)。問題是搜索不起作用。它似乎總是返回表中的所有記錄,而不是一個,這意味着where子句有問題,我試圖根據搜索模式窗口中給出的內容生成。或者,它可能是搜索的情況下根本沒有被激活..jQgrid搜索返回子句不起作用的所有記錄
這裏是電網代碼:
$("#list").jqGrid({
url: "server.php",
datatype: "json",
mtype: "POST",
colNames: ["Driver ID", "Name", "Country", "Victories", "Poles",
"Titles", "Fastest laps"], //"Team name"],
colModel: [
{ name: "driverid", index:"driverid", width: 55, search:true, stype:'text', editable:false},
{ name: "name", index:"name", width: 90, search:true, stype:'text', editable:true },
{ name: "country", index:"country", width: 90, search:true, stype:'text', editable:true },
{ name: "victories", index:"victories", width: 80, search:true, align: "right", editable:true },
{ name: "poles", index:"poles", width: 80, search:true, align: "right", editable:true },
{ name: "titles", index:"titles", width: 80, search:true, align: "right", editable:true },
{ name: "flaps", index:"flaps", width: 80, search:true, align: "right", editable:true}
//{ name: "teamName", width: 80, align: "right", editable:true}
],
autowidth: true,
pager: "#pager",
rowNum: 10,
rowList: [10, 20, 30],
sortname: "driverid",
sortorder: "asc",
viewrecords: true,
gridview: true,
autoencode: true,
caption: "F1 statistics grid",
editurl: "dbedit.php"
});
jQuery("#list").jqGrid('navGrid','#pager',{add:true,del:true,edit:true,search:true});
});
這裏是dbedit.php部分:
include("connection.php"); //
$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
// select the database
mysql_select_db($database) or die("Error connecting to db.");
$action="";
if (isset($_POST['oper']))
{
$action=$_POST['oper'];
}
else
{
echo "Definisi akciju";
exit();
}
switch ($action)
{
case "add":
{
$driverid = mysql_real_escape_string($_POST['driverid']);
$name = mysql_real_escape_string($_POST['name']);
$country = mysql_real_escape_string($_POST['country']);
$victories = mysql_real_escape_string($_POST['victories']);
$poles = mysql_real_escape_string($_POST['poles']);
$titles = mysql_real_escape_string($_POST['titles']);
$flaps = mysql_real_escape_string($_POST['flaps']);
//$teamName = mysql_real_escape_string($_POST['teamName']);
//$insert = 'INSERT INTO '.'drivers';
//$insert .= ' VALUES (NULL,';
//$insert .="'".$name."','".$country."', '".$victories."','".$poles."', '".$titles."', '".$flaps."')";
$insert = "INSERT INTO drivers (driverid, name, country, victories, poles, titles, flaps) VALUES (Null,'".$name."','".$country."','".$victories."','".$poles."','".$titles."','".$flaps."')";
$result = mysql_query($insert) or die("Couldn't execute query.".mysql_error());
//$_SESSION['nesto'] = $result;
}
break;
case "edit":
{
$driverid = mysql_real_escape_string($_POST['id']);
$name = mysql_real_escape_string($_POST['name']);
$country = mysql_real_escape_string($_POST['country']);
$victories = mysql_real_escape_string($_POST['victories']);
$poles = mysql_real_escape_string($_POST['poles']);
$titles = mysql_real_escape_string($_POST['titles']);
$flaps = mysql_real_escape_string($_POST['flaps']);
$update = 'UPDATE drivers'." SET name='". $name ."', country='" . $country ."', victories='" . $victories ."', poles='" . $poles ."', titles='" . $titles ."', flaps='" . $flaps. "' WHERE driverid=". $driverid ;
//echo $update;
$result = mysql_query($update) or die("Couldn't execute query.".mysql_error());
}
break;
case "del":
{
$driverid = mysql_real_escape_string($_POST['id']); //id reda iz grida
$delete = 'DELETE FROM drivers WHERE driverid='.$driverid;
$result = mysql_query($delete) or die("Couldn't execute query.".mysql_error());
}
break;
case "search":
{
$page = $_POST['page'];
// get how many rows we want to have into the grid - rowNum parameter in the grid
$limit = $_POST['rows'];
//echo $limit;
// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel
$sidx = $_POST['sidx'];
// sorting order - at first time sortorder
$sord = $_POST['sord'];
// if we not pass at first time index use the first column for the index or what you want
if(!$sidx) $sidx =1;
$ops = array(
'eq'=>'=', //equal
'ne'=>'<>',//not equal
'lt'=>'<', //less than
'le'=>'<=',//less than or equal
'gt'=>'>', //greater than
'ge'=>'>=',//greater than or equal
'bw'=>'LIKE', //begins with
'bn'=>'NOT LIKE', //doesn't begin with
'in'=>'LIKE', //is in
'ni'=>'NOT LIKE', //is not in
'ew'=>'LIKE', //ends with
'en'=>'NOT LIKE', //doesn't end with
'cn'=>'LIKE', // contains
'nc'=>'NOT LIKE' //doesn't contain
);
function getWhereClause($col, $oper, $val){
global $ops;
//if($oper == 'eq' || $oper == 'ne') $val .= "";
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($_POST['searchField']) ? $_POST['searchField'] : false;
$searchOper = isset($_POST['searchOper']) ? $_POST['searchOper']: false;
$searchString = isset($_POST['searchString']) ? $_POST['searchString'] : false;
if ($_POST['_search'] == 'true') {
$where = getWhereClause($searchField,$searchOper,$searchString);
}
// connect to the MySQL database server
$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
// select the database
mysql_select_db($database) or die("Error connecting to db.");
// calculate the number of rows for the query. We need this for paging the result
$result = mysql_query("SELECT COUNT(*) AS count FROM drivers ".$where.")";
//$result = mysql_query("SELECT COUNT(*) AS count FROM drivers a, teams b WHERE "."a.driverid=b.driver1id"."");
//echo $result1;
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
//$_SESSION['nesto'] = $count;
// calculate the total pages for the query
if($count > 0 && $limit > 0) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
// if for some reasons the requested page is greater than the total
// set the requested page to total page
if ($page > $total_pages) $page=$total_pages;
// calculate the starting position of the rows
$start = $limit*$page - $limit;
// if for some reasons start position is negative set it to 0
// typical case is that the user type 0 for the requested page
if($start <0) $start = 0;
// the actual query for the grid data
$SQL = "SELECT driverid, name, country, victories, poles, titles, flaps FROM drivers ".$where;
//$SQL = "SELECT "."a.driverid, a.name, a.country, a.victories, a.poles, a.titles, a.flaps, b.teamName"." FROM drivers a, teams b WHERE a.driverid=b.driver1id OR a.driverid=b.driver2id ORDER BY $sidx $sord LIMIT $start , $limit";
//echo $SQL;
$result = mysql_query($SQL) or die("Couldn't execute query.".mysql_error());
//$result = mysql_query($SQL) or die("Couldn't execute query.".mysql_error());
$response=null;
$response->page = $page;
$response->total = $total_pages;
$response->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$response->rows[$i]['id']=$row['driverid'];
$response->rows[$i] ['cell']=array($row['driverid'],$row['name'],$row['country'],$row['victories'],$row['poles'],$row['titles'],$row['flaps']);//,$row['teamName']);
$i++;
}
echo json_encode($response);
}
我已經完成了20次代碼,看看我是否輸入了錯誤的東西,但看不到它。是我在定義網格還是colModel時錯過的東西?
謝謝你的回答,我試過了,它沒有工作,因爲我錯過了一件至關重要的事情,那就是搜索是一個簡單的數據顯示,這意味着管理搜索的所有代碼應該位於url文件中,在我的情況下是server.php。現在它工作正常。 – Matija
等等,所以你離開'case:「搜索」{}'空?你把所有的代碼放在server.php文件中?如果沒有,你能解釋一下,你在server.php中放什麼以及在什麼情況下「搜索」?我也想這樣做,但沒有我嘗試的作品。 – j3ca