dongou5100 2015-02-25 22:34
浏览 110
已采纳

jqgrid工具栏搜索没有获取mysql的过滤器

this may have been answered before but i have not found something working for me.

i am trying to implement the toolbar search option for jqgrid with php/json data.

so far i have the following

<script src="js/jquery-1.11.0.min.js"></script>
<script type='text/javascript' src='js/jquery-ui-1.8.4.custom.min.js'></script>        
<script type='text/javascript' src='js/i18n/grid.locale-es.js'></script>
<script type='text/javascript' src='js/jquery.jqGrid.min.js'></script>

<script>
$(document).ready(function () {
$("#list_records").jqGrid({
url: "grid.php",
datatype: "json",
mtype: "GET",
colNames: ["ID", "Encuesta", "Fecha", "Quien"],
colModel: [
{ name: "id_consulta"},
{ name: "texto"},
{ name: "fecha"},
{ name: "quien"}
],
pager: "#perpage",
rowNum: 10,
rowList: [10,20],
sortname: "id_consulta",
sortorder: "asc",
height: 'auto',
viewrecords: true,
gridview: true,
caption: ""
});

$('#list_records').jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch : "bw"});

});
</script>

i set the stringresult: true and tried to get the filters.

the correpsonding php file is

$db = mysql_connect($server,$user,$pass);
mysql_select_db($dbname,$db);

    $page = $_REQUEST['page']; // get the requested page
    $limit = $_REQUEST['rows']; // get how many rows we want to have into the grid
    $sidx = $_REQUEST['sidx']; // get index row - i.e. user click to sort
    $sord = $_REQUEST['sord']; // get the direction
    if(!$sidx) $sidx =1;


$filterResultsJSON = json_decode($_REQUEST['filters']);

$filterArray = get_object_vars($filterResultsJSON);

$counter = 0;
$sql='';
while($counter < count($filterArray['rules']))
{
$filterRules = get_object_vars($filterArray['rules'][$counter]);

if($counter == 0){
$sql .= ' WHERE ' . $filterRules['field'] . ' LIKE "%' . $filterRules['data'] . '%"';
}
else {
$sql .= ' AND ' . $filterRules['field'] . ' LIKE "%' . $filterRules['data'] . '%"';
}
$counter++;
}

$totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false;
if($totalrows) {$limit = $totalrows;}

$result = mysql_query("SELECT COUNT(*) AS count FROM consulta $sql"); 
$row = mysql_fetch_array($result,MYSQL_ASSOC); 

$count = $row['count']; 
if( $count > 0 && $limit > 0) { 
$total_pages = ceil($count/$limit); 
} else { 
$total_pages = 0; 
} 
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit;
if($start <0) $start = 0; 

$SQL = "SELECT * FROM consulta $sql ORDER BY $sidx $sord LIMIT $start , $limit"; 
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); 

$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$responce->rows[$i]['id']=$row['id_consulta'];
$responce->rows[$i]['cell']=array($row['id_consulta'],utf8_encode($row['texto']),$row['fecha'],$row['quien']);
$i++;
}

echo json_encode($responce);

i am getting the following error message upon calling the php directly Warning: get_object_vars() expects parameter 1 to be object, null given in /home/www2w/public_html/tuvoto/grid.php on line 16 Couldn't execute query.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

any help apreciated

  • 写回答

2条回答 默认 最新

  • dongsheng9203 2015-02-27 10:36
    关注

    while loading grid first time you are not searching anything.. so filters are set null.. and in get_object_vars(), we cant pass null values. so put condition there,

    if(filterResultsJSON!="")
    {
    $filterArray = get_object_vars($filterResultsJSON);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用