不使用jqgrid上的GET方法从服务器调用数据

时间:2015-06-17 22:45:04

标签: php jquery ajax json jqgrid

我是jqgrid的新手。我做了很多关于如何在不使用GET方法的情况下从数据库调用数据的研究,但没有成功。

我的HTML代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>jqGrid UI</title>
    <link rel='stylesheet' type='text/css' href='http://code.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css' />
    <link rel='stylesheet' type='text/css' href='http://www.trirand.com/blog/jqgrid/themes/ui.jqgrid.css' />

    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

    <script type='text/javascript' src='http://www.trirand.com/blog/jqgrid/js/jquery-ui-custom.min.js'></script>        
    <script type='text/javascript' src='http://www.trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js'></script>
    <script type='text/javascript' src='http://www.trirand.com/blog/jqgrid/js/jquery.jqGrid.js'></script>

    <script>
    $(document).ready(function () {
        $("#list_records").jqGrid({
            url: "getGridData.php", // I want to change this to be on the same page to call the data from the database, do I have to use the data: varname?
            datatype: "json",
            mtype: "GET", // should I change this to data type: local if I want to change the url?
            colNames: ["User Id", "User Name", "First Name", "Last Name"],
            colModel: [
                { name: "invid",align:"right"},
                { name: "invdate"},
                { name: "amount"},
                { name: "tax"}
            ],
            pager: "#perpage",
            rowNum: 10,
            rowList: [10,20],
            sortname: "invid",
            sortorder: "asc",
            height: 'auto',
            viewrecords: true,
            gridview: true,
            caption: ""
        });     
    });
    </script>
</head>

<body>
<table id="list_records"><tr><td></td></tr></table> 
<div id="perpage"></div>
</body>
</html>

我的php mysql代码如下:

<?php 
$conn = mysql_connect("localhost", "root", "") or die("Connection Error: " . mysql_error()); 
mysql_select_db("jqgrid") or die("Error connecting to db."); 

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

if(!$sidx) $sidx =1; 

$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 * FROM invheader ORDER BY $sidx $sord LIMIT $start , $limit"; 
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); 

$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $responce->rows[$i]['id']=$row['invid'];
    $responce->rows[$i]['cell']=array($row['invdate'],$row['amount'],$row['tax'],$row['total']);
    $i++;
}
echo json_encode($responce);
?>

这是完全有效的。但是,我不想使用getmethod。我想把它全部放在一个php页面中。有人可以给我一个示例代码,说明如何使用数组实现此目的。提前谢谢!

更新

我现在已经设法找到如何按照此处给出的说明http://www.dyn-web.com/tutorials/php-js/json/array.php

将php查询结果转换为javascript

我现在的问题是我无法将转换后的变量用于jqgrid。我想在这个页面http://trirand.com/blog/jqgrid/jqgrid.html#t95上得到相同的结果,唯一不同的是我使用转换的php结果到javascropt而不是创建静态数据。

以下是我的脚本代码:

var mydata = <?php echo json_encode($dataitems)?>;
jQuery("#list4").jqGrid({
    data: mydata,
    datatype: "local",
    height: 250,
    colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],
    colModel:[
        {name:'id',index:'id', width:60, sorttype:"int"},
        {name:'invdate',index:'invdate', width:90, sorttype:"date"},
        {name:'name',index:'name', width:100},
        {name:'amount',index:'amount', width:80, align:"right",sorttype:"float"},
        {name:'tax',index:'tax', width:80, align:"right",sorttype:"float"},     
        {name:'total',index:'total', width:80,align:"right",sorttype:"float"},      
        {name:'note',index:'note', width:150, sortable:false}       
    ],
    multiselect: true,
    caption: "Manipulating Array Data"
});

1 个答案:

答案 0 :(得分:1)

虽然这不是非常有效,但你可以做这样的事情

$data = array();
while($row = mysql_fetch_assoc($result)) $data[] = $row;
echo '<script>var data =' . json_encode($data) . ';</script>';

此时,您将拥有一个包含所有数据的JSON对象。这将使页面加载时间更长,但会在没有AJAX的情况下执行您的要求

虽然我在,mysql_ functions are deprecated。 Copnsider切换到mysqli_