简体   繁体   English

从php文件返回的jqGrid搜索数据

[英]jqGrid search data returned from php file

i ve post this question in programmers.stackexchange.com also and they told me that it was off topic there. 我也把这个问题发布在programmers.stackexchange.com上,他们告诉我那不是主题。 So..let me tell you the story. 所以..让我告诉你这个故事。

Yesterday , after reading the documentation (www.trirand.com/jqgridwiki/doku.php) of jqgrid i created a simple table that gets data from a xml file returned by a php file. 昨天,在阅读了jqgrid的文档(www.trirand.com/jqgridwiki/doku.php)之后,我创建了一个简单的表,该表从php文件返回的xml文件中获取数据。

In particular here is the code. 特别是这里的代码。

index.html and example.php index.htmlexample.php

As an example I use this one , but the problem is that I cannot make the select search method work in this way. 作为示例,我使用了这一示例,但是问题是我无法使select搜索方法以这种方式工作。 I thought to use dataUrl in editoptions but in the wiki says that this works only with HTML files. 我以为在editoptions中使用dataUrl,但是在Wiki中说这仅适用于HTML文件。

I can't imagine a way to manipulate the data of the xml file to make the dropdown menu work. 我无法想象一种方法来处理xml文件的数据以使下拉菜单正常工作。

How to make it work? 如何使其运作?

Finally i solve this problem with the help of this code: 最后,我借助以下代码解决了这个问题:

<?php
$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction
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
    '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 == '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);
}

// connect to the database
$dbhost = "host_address";
$dbuser = "db_user";
$dbpassword = "db_pass";
$database = "db_name";
$tablename
$db = mysql_connect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());

mysql_select_db($database) or die("Error conecting to db.");
mysql_set_charset('utf8',$database);
mysql_query("SET NAMES 'utf8'");
$result = mysql_query("SELECT COUNT(*) AS count FROM $tablename");
$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)
$SQL = "SELECT field1, field2, field3, field4, field5 FROM $tablename "
.$where." ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn?t execute query.".mysql_error());

if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
header("Content-type: text/xml;charset=utf-8");
}
$et = ">";

echo "<?xml version='1.0' encoding='utf-8'?$et\n";
echo "<rows>";
echo "<page>".$page."</page>";
echo "<total>".$total_pages."</total>";
echo "<records>".$count."</records>";
// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    echo "<row id='". $row[field1]."'>";
    echo "<cell>". $row[field1]."</cell>";
    echo "<cell>". $row[field2]."</cell>";
    echo "<cell><![CDATA[". $row[field3]."]]></cell>";
    echo "<cell>". $row[field4]."</cell>";
    echo "<cell>". $row[field5]."</cell>";
    echo "</row>";
}
echo "</rows>";
?>

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM