【问题标题】:jqgrid not displaying php json datajqgrid不显示php json数据
【发布时间】:2013-06-02 03:11:23
【问题描述】:

我的 jqgrid 没有显示从我的 php 文件返回的 json 数据。 php 文件生成 json 数据>正确,但我的 jqgrid 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" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Address</title>

  <link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery- ui-1.10.3.custom.css" />
 <link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />

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

 $(function () {
 $("#list").jqGrid({
     url: 'example.php',
     datatype: 'json',
    mtype: "GET",
    colNames: ["addressid", "buildingname", "street", "cityid", "countryid",    "statteid","pincode","phone","mobile","fax"],
     colModel: [
         { name: "addressid", width: 55 },
         { name: "buildingname", width: 90 },
         { name: "street", width: 80, align: "right" },
         { name: "cityid", width: 80, align: "right" },
         { name: "countryid", width: 80, align: "right" },
         { name: "stateid", width: 150, align: "right" },
     { name: "pincode", width: 150, align: "right" },
         { name: "phone", width: 150, align: "right" },
         { name: "mobile", width: 150, align: "right" },
         { name: "fax", width: 150, align: "right" }

     ],
     pager: "#pager",
     rowNum: 10,
     rowList: [10, 20, 30],
     sortname: "invid",
     sortorder: "desc",
     viewrecords: true,
     gridview: true,
     autoencode: true,
     caption: "Address"
   }); 
  }); 
 </script>


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

example.php

<?php

// initialization
 $dbhost = "";
 $dbuser = "root";
 $dbpassword = "ERTFS645@#";
 $database = "dbeg";

// connect to the database
 $db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: ");
 mysql_select_db($database) or die("Database connection error.");

// get the count of rows
 $result = mysql_query("SELECT * FROM Address");
 $row = mysql_fetch_array($result, MYSQL_ASSOC);
 $count = $row['count'];

 // create a response array from the obtained result
 $i = 0;


while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

echo json_encode($row);

}

 mysql_close($db);
?>

example.php 的输出

{"addressid":"101","buildingname":"Sundale","street":"OTTP","cityid":"652","countryid":"6","statteid":"65 ","pincode":"656665","phone":"986346654","mobile":"823343454","fax":"554332"}

【问题讨论】:

    标签: php json jqgrid-php


    【解决方案1】:

    你的 json 应该看起来像

    {
        "page": 2,//current page
        "total": 2,//number of pages
        "records": 11,//# of records in total
        "rows": [//array of data
            {
                "id": "101",//id for this row of data
                    "cell": [
                    "101",
                    "Sundale",
                    "OTTP",
                    "652",
                    "6",
                    "65",
                    "656665",
                    "986346654",
                    "823343454",
                    "554332"
                ]
            }
        ]
    }
    

    http://www.trirand.com/blog/jqgrid/jqgrid.html

    【讨论】:

      【解决方案2】:
      <?php      $page =  $_GET['page']; // get the requested page
           $limit = $_GET['rows']; // get how many rows we want to have into the grid
           $sidx =  $_GET['sidx']; // get index row - i.e. user click to sort
           $sord =  $_GET['sord']; // get the direction
      
          $page = 2;
          $limit = 30;
         if(!$sidx) $sidx =1;
      
      // connect to the database
      $db = mysql_connect("localhost", "root", "luis1983") or die("Connection Error: " . mysql_error());
      mysql_select_db("wine") or die("Error conecting to db.");
      $result = mysql_query("SELECT COUNT(*) AS count FROM wine.wine");
      $row = mysql_fetch_array($result, MYSQL_ASSOC);
      $count = $row['count'];
      
      if( $count >0 ) {
          $total_pages = ceil($count/$limit);
      } else {
          $total_pages = 0; }
      if ($page > $total_pages) $page=$total_pages;
      $start = $limit*$page - $limit;
      $SQL = "SELECT wine_id, wine_name, wine_ENGINE,  winery_id, description FROM wine.wine LIMIT   30";
       $result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
      
        $responce = (object) array('page' => $page, 'total' => $total_pages, 'records' =>$count, 'rows' => "");
      
        $responce->page = $page;
        $responce->total = $total_pages;
        $responce->records = $count;
        $i=0;
         while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      
            $responce->rows[$i]['id']=$row['wine_id'];
             $responce->rows[$i]    ['cell']=array($row['wine_id'],$row['wine_name'],$row['wine_ENGINE'],$row['winery_id'],$row['d escription']);
          $i++;
        } 
        echo json_encode($responce);
      
         mysql_close($db);
        ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-09
        • 1970-01-01
        相关资源
        最近更新 更多