2013-02-12 110 views
1

我試圖使用沒有成功的JqGrid。我讀過this wiki post at trirand,並且我完成了該頁面上的所有步驟。Jqgrid不加載XML數據

我使用,我已經搜查,但我一直有同樣的錯誤等exaples嘗試。我真的不知道爲什麼,這很令人沮喪。

當我查看HTML,表(MySQL的)不顯示。這就像我的代碼不接受來自PHP代碼的XML數據。我正在使用Appserv。

HTML代碼(myfirstgrid.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>My First Grid</title> 
     <link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.8.2.custom.css" /> 
     <link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" /> 

     <style type="text/css"> 
      html, body { 
       margin: 0; 
       padding: 0; 
       font-size: 75%; 
      } 
     </style> 

     <script src="/jquery-1.7.2.min.js" type="text/javascript"></script> 
     <script src="/i18n/grid.locale-en.js" type="text/javascript"></script> 
     <script src="/jquery.jqGrid.min.js" type="text/javascript"></script> 

     <script type="text/javascript"> 
     $(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> 

PHP腳本(使用example.php):

<?php 
    include ("conexion.php"); 

    $page = 1; 
    $limit = 10; 
    $sidx = 'tax'; 
    $sord = 'asc'; 

    $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 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 invid, invdate, amount, tax,total, note FROM invheader 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['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; 
?> 

這是來自日PHP腳本的XML結果(在XML被縮進爲了提高可讀性的服務器,而無需換行返回XML):

<?xml version='1.0' encoding='utf-8'?> 
<rows> 
    <page>1</page> 
    <total>1</total> 
    <records>3</records> 
    <row id='1'> 
     <cell>1</cell> 
     <cell>0000-00-00</cell> 
     <cell>0.00</cell> 
     <cell>0.00</cell> 
     <cell>0.00</cell> 
     <cell><![CDATA[note]]></cell> 
    </row> 
    <row id='2'> 
     <cell>2</cell> 
     <cell>2001-01-10</cell> 
     <cell>103.00</cell> 
     <cell>45.00</cell> 
     <cell>149.00</cell> 
     <cell><![CDATA[This is record 1]]></cell> 
    </row> 
    <row id='3'> 
     <cell>3</cell> 
     <cell>2001-02-10</cell> 
     <cell>104.00</cell> 
     <cell>46.00</cell> 
     <cell>151.00</cell> 
     <cell><! [CDATA[This is record 2]]></cell> 
    </row> 
</rows> 

這是我的問題:

  1. 爲什麼不在HTML顯示我的數據?
  2. 如果問題是XML數據,什麼是檢查XML是否有效的最好方法?

回答

2

好吧,林找到了解決辦法。我廣泛修改了我的代碼。

問題是這樣的:

include ("conexion.php"); 

出於某種原因,如果你有其他文件這個文件在這部分所發送的報頭中的標頭前引黃參數:

header("Content-type: text/xml;charset=utf-8"); 

所以你需要在你的網格php文檔中設置這個參數,並確保你發送了這一行的標題。

就是這樣......

因此,許多人都有這樣的疑問,也許問題這是該解決方案。