2011-07-23 44 views
0

jqgridを使用してデータベースからデータを読み込むことができません。私はデモコードをwiki(http://www.trirand.com/jqgridwiki/doku.php?id=wiki:first_grid)で使っていますが、それはうまくいきますが、自分のデータベースから情報をロードしようとすると、仕事。jqGridにデータが表示されない

デモコードを使用して、私が最初に間違ったコードを書いたかどうかを確認するために使用したいテーブルに従って変更しましたが、動作しませんでした。

のindex.html:

<script src="js/jquery-1.5.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> 

<script type="text/javascript"> 
$(function(){ 
$("#list").jqGrid({ 
url:'dades.php', 
datatype: 'xml', 
mtype: 'GET', 
colNames:['Id','Nom', 'Email','Poblacio','Naixement','Tel'], 
colModel :[ 
{name:'id', index:'id', width:55}, 
{name:'nom', index:'nom', width:90}, 
{name:'email', index:'email', width:80, align:'right'}, 
{name:'poblacio', index:'poblacio', width:80, align:'right'}, 
{name:'naixement', index:'naixement', width:80, align:'right'}, 
{name:'tel', index:'tel', width:150, sortable:false} 
], 
pager: '#pager', 
rowNum:10, 
rowList:[10,20,30], 
sortname: 'id', 
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> 

dades.php:それは仕事dodn'tなぜここ

<?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 llista"); 
$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 id,nom,email,poblacio,naixement,tel FROM llista 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['id']."'>";    
    $s .= "<cell>". $row['id']."</cell>"; 
    $s .= "<cell>". $row['nom']."</cell>"; 
    $s .= "<cell>". $row['email']."</cell>"; 
    $s .= "<cell>". $row['poblacio']."</cell>"; 
    $s .= "<cell>". $row['naixement']."</cell>"; 
    $s .= "<cell>". $row['tel']."</cell>"; 
    $s .= "</row>"; 
} 
$s .= "</rows>"; 

echo $s; 
?> 

誰もが知っていますか?私が行っテストのために

(EDIT)

は、デ問題がGETの一部であるようだ。

$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; 

動作しないようですが、私は理解していませんなぜ。

(EDIT 2)

ので、私はそれなしでやってしまった、と大きな問題は非常にデータベースの文字を変え...データベースで間違っUTFにあったGETの一部が動作しないようです設定してGETすれば完璧に動作します。

+0

は、あなたのサーバーを生成するXMLデータが含まれてもらえますか? [Fiddler](http://www.fiddler2.com/fiddler2/)または[Firebug](http://getfirebug.com/)を使用して、サーバーのHTTP応答からデータを取得できます。さらに、HTTPヘッダの "Content-Type"の値を知ることも重要です。 – Oleg

+0

dades.phpのヘッダーは次のとおりです。 header( "Content-type:text/xml; charset = utf-8"); – kevinspencer33

+0

サーバーを生み出すXMLデータを含めることができますか? – Oleg

答えて

0

あなたは結果セットのカウントクエリを変更していません。それでもまた、あなたがHTMLテーブルにコンテンツを必要はありません

// calculate the number of rows for the query. We need this for paging the result 
$result = mysql_query("SELECT COUNT(*) AS count FROM llista"); 

にこれを更新する必要があり

// calculate the number of rows for the query. We need this for paging the result 
    $result = mysql_query("SELECT COUNT(*) AS count FROM invheader"); 

<table id="list"></table> 
+0

私はこれを今気付きましたが、試しましたが動作しません... – kevinspencer33

+0

colModelのcolNamesと列名が一致しません... – Andrea

+0

ありがとう、私はこれを編集します(愚かな間違いは悪いですスポット...)しかし、問題はまだここにあります。 – kevinspencer33

関連する問題