【问题标题】:Undefined index: page jqGrid未定义的索引:页面 jqGrid
【发布时间】:2014-04-10 11:41:25
【问题描述】:

我是 jqGrid 的新手。我正在尝试从我的网格中的数据库获取数据,但在我的 server.php 文件中出现错误未定义索引:页面,未定义索引:行,未定义索引:sidx,未定义索引:sord。我在 grid.html 上得到了网格,但没有来自数据库的数据。

我有 grid.html

...
<script type="text/javascript">
$(function () {
    $("#list").jqGrid({
        url: "server.php",
        datatype: "xml",
        mtype: "GET",
        colNames: ["Zaposleni ID", "Ime", "Prezime", "Godine staza", "Mesecna plata", "Godisnja plata", "Broj odradjenih projekata", "Kod zaposlenog", "Odeljenje"],
        colModel: [
            { name: "zap_id", width: 55 },
            { name: "ime", width: 90 },
            { name: "prezime", width: 90 },
            { name: "godine_staza", width: 80, align: "right" },
            { name: "mesecna_plata", width: 80, align: "right" },
            { name: "godisnja_plata", width: 80, align: "right" },
            { name: "broj_odradjenih_projekata", width: 50 },
            { name: "kod_zaposlenog", width: 50 },
            { name: "odeljenje", width: 150 }
        ],
        autowidth: true,
        pager: "#pager",
        rowNum: 10,
        rowList: [10, 20, 30],
        sortname: "invid",
        sortorder: "desc",
        viewrecords: true,
        gridview: true,
        autoencode: true,
        caption: "Zaposleni"
    }); 
}); 
</script>
...

dbconfig.php

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$database="kompanija";
?>

server.php

<?php 
//include the information needed for the connection to MySQL data base server. 
// we store here username, database and password 
include("dbconfig.php"); //

// to the url parameter are added 4 parameters as described in colModel
// we should get these parameters to construct the needed query
// Since we specify in the options of the grid that we will use a GET method 
// we should use the appropriate command to obtain the parameters. 
// In our case this is $_GET. If we specify that we want to use post 
// we should use $_POST. Maybe the better way is to use $_REQUEST, which
// contain both the GET and POST variables. For more information refer to php documentation.
// Get the requested page. By default grid sets this to 1. 
$page = $_GET['page']; 

// get how many rows we want to have into the grid - rowNum parameter in the grid 
$limit = $_GET['rows']; 

// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel 
$sidx = $_GET['sidx']; 

// sorting order - at first time sortorder 
$sord = $_GET['sord']; 

// if we not pass at first time index use the first column for the index or what you want
if(!$sidx) $sidx =1; 

// 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 zaposleni"); 
$row = mysql_fetch_array($result,MYSQL_ASSOC); 
$count = $row['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 zaposleni.zap_id, zaposleni.ime, zaposleni.prezime, zaposleni.godine_staza, zaposleni.mesecna_plata, zaposleni.godisnja_plata, zaposleni.broj_odradjenih_projekata, zaposleni.kod_zaposlenog, odeljenje.od_id FROM zaposleni INNER JOIN odeljenje ON zaposleni.od_id=odeljenje.od_id ORDER BY $sidx $sord LIMIT $start , $limit"; 
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); 

// we should set the appropriate header information. Do not forget this.
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>";

// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $s .= "<row id='". $row['zap_id']."'>";            
    $s .= "<cell>". $row['zap_id']."</cell>";
    $s .= "<cell>". $row['ime']."</cell>";
    $s .= "<cell>". $row['prezime']."</cell>";
    $s .= "<cell>". $row['godine_staza']."</cell>";
    $s .= "<cell>". $row['mesecna_plata']."</cell>";
    $s .= "<cell>". $row['godisnja_plata']."</cell>";
    $s .= "<cell>". $row['broj_odradjenih_projekata']."</cell>";
    $s .= "<cell>". $row['kod_zaposlenog']."</cell>";
    $s .= "<cell>". $row['od_id']."</cell>";
    $s .= "</row>";
}
$s .= "</rows>"; 

echo $s;

?>

有人可以帮帮我吗?我找不到问题。

【问题讨论】:

  • 你是否导入了所需的jquery库?

标签: javascript php mysql sql jqgrid


【解决方案1】:

排序名称:“invid”

您的网格模型中没有这样的列。

排序名称:“zap_id

并为所有列设置索引,像这样

{ name: "zap_id",index:"zap_id" , width: 55 }

【讨论】:

  • 谢谢!这对我有用!我从我的数据库中获取数据,这是最大的问题。但是我打开server.php时仍然出错,它仍然是相同的未定义索引:页面,行......为什么?
  • 好吧,为了让 server.php 不会出现任何错误,当您尝试从网格加载没有 ajax 数据的情况下,您需要为这 4 个变量设置默认值: $page = $_GET['页']:1; $limit = $_GET['rows']:100; $sidx = $_GET['sidx']:'zap_id'; $sord = $_GET['sord']:'asc';
  • 好的,我明白了。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 2016-12-29
  • 1970-01-01
  • 2015-12-08
  • 2012-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多