jqgrid - $ page无效

时间:2012-05-19 21:39:42

标签: php javascript jqgrid

我有问题... $ page = $ _request ['page']; ......是行不通的。

当我输入localhost / example2.php时,我得到了这个......

<?xml version='1.0' encoding='utf-8'?>
    <rows>
        <page></page>
        <total>0</total>
        <records>1</records>
        <row id='1'>
            <cell>1</cell>
            <cell>2001-01-10</cell>
            <cell>103.98</cell>
            <cell>45.34</cell>
            <cell>149.32</cell>
            <cell><![CDATA[xxx]]></cell>
        </row>
    </rows>

为什么$ page为null?


这是我的代码(从jqgrid wiki复制)......

的index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP jqGrid Class Example</title> 

    <!-- installation files of jqgrid -->
    <link rel="stylesheet" type="text/css" media="screen" href="themes/redmond/jquery-ui-1.8.16.custom.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="themes/ui.jqgrid.css" />
    <script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
    <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
    <!-- installation files of jqgrid -->

<script type="text/javascript">
    jQuery(document).ready(function(){
      $("#list").jqGrid({
        url:'example.php',
        datatype: 'xml',
        mtype: 'GET',
        colNames:['Inv No','Date', 'Amount','Tax','Total','Notes'],
        colModel :[ 
          {name:'invid', index:'invid', width:55}, 
          {name:'invdate', index:'invdate', width:90}, 
          {name:'amount', index:'amount', width:80, align:'right'}, 
          {name:'tax', index:'tax', width:80, align:'right'}, 
          {name:'total', index:'total', width:80, align:'right'}, 
          {name:'note', index:'note', width:150, sortable:false} 
        ],
        pager: '#pager',
        rowNum:10,
        rowList:[10,20,30],
        sortname: 'invid',
        sortorder: 'desc',
        viewrecords: true,
        gridview: true,
        caption: 'My first grid'
      }); 
    });
</script>

</head>
<body>
<table id="list"><tr><td/></tr></table> 
<div id="pager"></div> 
</body>
</html>

使用example.php

<?php
    $page = $_GET['page'];
    $limit = $_GET['rows'];
    $sidx = $_GET['sidx'];
    $sord = $_GET['sord'];

    if(!$sidx) $sidx =1; 

    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpassword = '1234';
    $database = 'northwind';

    $db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error()); 
    mysql_select_db($database) or die("Error connecting to db."); 

    $result = mysql_query("SELECT COUNT(*) AS count FROM invheader"); 
    $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 invid, invdate, amount, tax,total, note FROM invheader ORDER BY $sidx $sord LIMIT $start , $limit"; 
    $SQL = "SELECT invid, invdate, amount, tax,total, note FROM invheader"; 
    $result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); 

    header("Content-type: text/xml;charset=utf-8");
    $s = "<?xml version='1.0' encoding='utf-8'?>";
    $s .=  "<rows>";
    $s .= "<page>".$page."</page>";
    $s .= "<total>".$total_pages."</total>";
    $s .= "<records>".$count."</records>";

    while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
        $s .= "<row id='". $row['invid']."'>";            
        $s .= "<cell>". $row['invid']."</cell>";
        $s .= "<cell>". $row['invdate']."</cell>";
        $s .= "<cell>". $row['amount']."</cell>";
        $s .= "<cell>". $row['tax']."</cell>";
        $s .= "<cell>". $row['total']."</cell>";
        $s .= "<cell><![CDATA[". $row['note']."]]></cell>";
        $s .= "</row>";
    }
    $s .= "</rows>"; 

    echo $s;
?>

  

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

如果在example2.php中你有:

$page = $_request['page'];
正如你在帖子中所说的那样,它不起作用,因为它应该是:

$page = $_REQUEST['page'];