Php jqGrid:如何使用单一搜索功能?
如何使用jqGrid的单一搜索功能 我在“3.7版中的新版本”下以“单一搜索”为例 我的脚本几乎完全可以保存用于从数据库检索数据的查询,但我的脚本不进行过滤 有人能给我举一个使用PHP的单一搜索功能的完整例子吗 我还有一个关于演示页面中脚本的问题Php jqGrid:如何使用单一搜索功能?,php,jquery,search,jqgrid,jqgrid-php,Php,Jquery,Search,Jqgrid,Jqgrid Php,如何使用jqGrid的单一搜索功能 我在“3.7版中的新版本”下以“单一搜索”为例 我的脚本几乎完全可以保存用于从数据库检索数据的查询,但我的脚本不进行过滤 有人能给我举一个使用PHP的单一搜索功能的完整例子吗 我还有一个关于演示页面中脚本的问题 jQuery如何将条件传递给PHP以过滤结果?我注意到在查询中有一个变量$where,但该变量没有在PHP脚本中声明 我用MySQL数据库制作了一个示例。 首先,创建一个“测试”数据库、一个“项目”表,并插入一些记录: CREATE DATABASE
$where
,但该变量没有在PHP脚本中声明我用MySQL数据库制作了一个示例。
首先,创建一个“测试”数据库、一个“项目”表,并插入一些记录:
CREATE DATABASE IF NOT EXISTS test;
CREATE TABLE IF NOT EXISTS `items` (
`item_id` int(11) NOT NULL,
`item` varchar(255) DEFAULT '',
`item_cd` varchar(255) DEFAULT '',
PRIMARY KEY (`item_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
LOCK TABLES `items` WRITE;
INSERT INTO `items` VALUES (1,'Lorem','575878');
INSERT INTO `items` VALUES (2,'Lorem','857450');
INSERT INTO `items` VALUES (3,'ipsum','292404');
INSERT INTO `items` VALUES (4,'dolor','814131');
INSERT INTO `items` VALUES (5,'sit','962077');
INSERT INTO `items` VALUES (6,'amet,','549801');
INSERT INTO `items` VALUES (7,'sed','166822');
INSERT INTO `items` VALUES (8,'in','616758');
INSERT INTO `items` VALUES (9,'id','550799');
INSERT INTO `items` VALUES (10,'dictum','763004');
INSERT INTO `items` VALUES (11,'velit','244985');
INSERT INTO `items` VALUES (12,'est','525127');
INSERT INTO `items` VALUES (13,'suspendisse,','351690');
INSERT INTO `items` VALUES (14,'mauris','655061');
INSERT INTO `items` VALUES (15,'eget','423779');
INSERT INTO `items` VALUES (16,'imperdiet','493615');
INSERT INTO `items` VALUES (17,'ut,','864029');
INSERT INTO `items` VALUES (18,'mauris','546065');
INSERT INTO `items` VALUES (19,'vestibulum,','562819');
INSERT INTO `items` VALUES (20,'sagittis','238043');
INSERT INTO `items` VALUES (21,'ac.','867508');
INSERT INTO `items` VALUES (22,'Mauris','674897');
INSERT INTO `items` VALUES (23,'id','288097');
INSERT INTO `items` VALUES (24,'quam,','889530');
UNLOCK TABLES;
接下来,创建html页面“singlesearch.html”:
jQuery(文档).ready(函数(){
jQuery(“#single”).jqGrid({
url:'localset.php',
数据类型:“json”,
身高:255,
宽度:600,
colNames:['Index'、'Name'、'Code'],
colModel:[
{名称:'item_id',索引:'item_id',宽度:65,排序类型:'int'},
{名称:'item',索引:'item',宽度:150},
{名称:'item_cd',索引:'item_cd',宽度:100}
],
rowNum:50,
总数:2000,
行列表:[20,30,50],
loadonce:false,
mtype:“获取”,
行数:对,
行宽:40,
gridview:没错,
传呼机:“#psingle”,
sortname:'item_id',
viewrecords:是的,
分拣员:“asc”,
标题:“单一搜索本地数据”,
loadError:函数(jqXHR、textStatus、errorshown){
/*
警报('HTTP状态代码:'+jqXHR.status+'\n'+
'textStatus:'+textStatus+'\n'+
'ErrorSprown:'+ErrorSprown);
警报('HTTP消息正文(jqXHR.responseText):'+'\n'+jqXHR.responseText);
*/
警报(jqXHR.responseText);
}
});
jQuery(“#single”).jqGrid(
“导航栅格”,
#psingle#,
{del:false,add:false,edit:false},
{},//编辑的默认设置
{},//添加的默认设置
{}, //
{closeAfterSearch:true},//搜索选项
{}//视图参数
);
});
然后创建一个PHP页面“localset.PHP”:我用MySQL数据库制作了一个示例。
首先,创建一个“测试”数据库、一个“项目”表,并插入一些记录:
CREATE DATABASE IF NOT EXISTS test;
CREATE TABLE IF NOT EXISTS `items` (
`item_id` int(11) NOT NULL,
`item` varchar(255) DEFAULT '',
`item_cd` varchar(255) DEFAULT '',
PRIMARY KEY (`item_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
LOCK TABLES `items` WRITE;
INSERT INTO `items` VALUES (1,'Lorem','575878');
INSERT INTO `items` VALUES (2,'Lorem','857450');
INSERT INTO `items` VALUES (3,'ipsum','292404');
INSERT INTO `items` VALUES (4,'dolor','814131');
INSERT INTO `items` VALUES (5,'sit','962077');
INSERT INTO `items` VALUES (6,'amet,','549801');
INSERT INTO `items` VALUES (7,'sed','166822');
INSERT INTO `items` VALUES (8,'in','616758');
INSERT INTO `items` VALUES (9,'id','550799');
INSERT INTO `items` VALUES (10,'dictum','763004');
INSERT INTO `items` VALUES (11,'velit','244985');
INSERT INTO `items` VALUES (12,'est','525127');
INSERT INTO `items` VALUES (13,'suspendisse,','351690');
INSERT INTO `items` VALUES (14,'mauris','655061');
INSERT INTO `items` VALUES (15,'eget','423779');
INSERT INTO `items` VALUES (16,'imperdiet','493615');
INSERT INTO `items` VALUES (17,'ut,','864029');
INSERT INTO `items` VALUES (18,'mauris','546065');
INSERT INTO `items` VALUES (19,'vestibulum,','562819');
INSERT INTO `items` VALUES (20,'sagittis','238043');
INSERT INTO `items` VALUES (21,'ac.','867508');
INSERT INTO `items` VALUES (22,'Mauris','674897');
INSERT INTO `items` VALUES (23,'id','288097');
INSERT INTO `items` VALUES (24,'quam,','889530');
UNLOCK TABLES;
接下来,创建html页面“singlesearch.html”:
jQuery(文档).ready(函数(){
jQuery(“#single”).jqGrid({
url:'localset.php',
数据类型:“json”,
身高:255,
宽度:600,
colNames:['Index'、'Name'、'Code'],
colModel:[
{名称:'item_id',索引:'item_id',宽度:65,排序类型:'int'},
{名称:'item',索引:'item',宽度:150},
{名称:'item_cd',索引:'item_cd',宽度:100}
],
rowNum:50,
总数:2000,
行列表:[20,30,50],
loadonce:false,
mtype:“获取”,
行数:对,
行宽:40,
gridview:没错,
传呼机:“#psingle”,
sortname:'item_id',
viewrecords:是的,
分拣员:“asc”,
标题:“单一搜索本地数据”,
loadError:函数(jqXHR、textStatus、errorshown){
/*
警报('HTTP状态代码:'+jqXHR.status+'\n'+
'textStatus:'+textStatus+'\n'+
'ErrorSprown:'+ErrorSprown);
警报('HTTP消息正文(jqXHR.responseText):'+'\n'+jqXHR.responseText);
*/
警报(jqXHR.responseText);
}
});
jQuery(“#single”).jqGrid(
“导航栅格”,
#psingle#,
{del:false,add:false,edit:false},
{},//编辑的默认设置
{},//添加的默认设置
{}, //
{closeAfterSearch:true},//搜索选项
{}//视图参数
);
});
然后创建一个PHP页面“localset.PHP”:
谢谢您的回复。我有几个问题。jQuery从哪里获得_search、searchString和search Oper的值?2.%s的含义是什么?有关_搜索的描述,请查看searchString和searchOper参数。“%s”是使用函数打印字符串的占位符。感谢您的回复。我有几个问题。jQuery从哪里获得_search、searchString和search Oper的值?2.%s的含义是什么?有关_搜索的描述,请查看searchString和searchOper参数。“%s”是使用函数打印字符串的占位符。
<?php
function myTrace($str) {
// Adjust the log file name before uncommenting
//@file_put_contents("/tmp/localset.log", $str."\n", FILE_APPEND);
}
myTrace(print_r($_REQUEST,true));
// connect to the database
$link = mysqli_init();
// Adjust host, user, password and dbname before use!
$db = mysqli_real_connect($link, "myhost", "myuser", "mypass", "test");
if (!$db) {
die('Connect Error ('.mysqli_connect_errno().'): '.mysqli_connect_error());
}
mysqli_set_charset($link, "utf8");
$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 0; // get the requested page
$limit = isset($_REQUEST['rows']) ? $_REQUEST['rows'] : 50; // get how many rows we want to have into the grid
$sidx = isset($_REQUEST['sidx']) ? $_REQUEST['sidx'] : 1; // get index row - i.e. user click to sort
$sord = isset($_REQUEST['sord']) ? $_REQUEST['sord'] : "asc"; // get the direction
if ($_REQUEST["_search"] == "false") {
$where = "where 1";
} else {
$operations = array(
'eq' => "= '%s'", // Equal
'ne' => "<> '%s'", // Not equal
'lt' => "< '%s'", // Less than
'le' => "<= '%s'", // Less than or equal
'gt' => "> '%s'", // Greater than
'ge' => ">= '%s'", // Greater or equal
'bw' => "like '%s%%'", // Begins With
'bn' => "not like '%s%%'", // Does not begin with
'in' => "in ('%s')", // In
'ni' => "not in ('%s')", // Not in
'ew' => "like '%%%s'", // Ends with
'en' => "not like '%%%s'", // Does not end with
'cn' => "like '%%%s%%'", // Contains
'nc' => "not like '%%%s%%'", // Does not contain
'nu' => "is null", // Is null
'nn' => "is not null" // Is not null
);
$value = mysqli_real_escape_string($link, $_REQUEST["searchString"]);
$where = sprintf("where %s ".$operations[$_REQUEST["searchOper"]], $_REQUEST["searchField"], $value);
}
$SQL = "SELECT item_id, item, item_cd FROM items ".$where." ORDER BY $sidx $sord";
myTrace($SQL);
$result = mysqli_query($link, $SQL);
if (!$result) {
myTrace(mysqli_error($link));
die("Couldn't execute query: ".mysqli_error($link));
}
if ($limit < 0) $limit = 0;
$start = ($limit * $page) - $limit;
if ($start < 0) $start = 0;
$count = mysqli_num_rows($result);
if ($count > 0) {
$total_pages = ceil($count / $limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) {
$page = $total_pages;
}
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
mysqli_data_seek($result, $start);
for ($i = 0; $row = mysqli_fetch_assoc($result); $i++) {
if (($limit > 0) && ($i >= $limit)) break;
$responce->rows[$i]['id'] = $row['item_id'];
$responce->rows[$i]['cell'] = array($row['item_id'], $row['item'], $row['item_cd']);
}
echo json_encode($responce);
mysqli_close($link);
?>