调用服务器上的数据,而不使用在jqGrid的GET方法 [英] Call data from the server without using GET Method on jqgrid

查看:200
本文介绍了调用服务器上的数据,而不使用在jqGrid的GET方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来jq​​Grid的。我做了很多关于如何调用从数据库中的数据的研究,而无需使用GET方法都没有成功。

我有以下HTML code:

 <!DOCTYPE HTML>
< HTML>
< HEAD>
    <元字符集=utf-8>
    <冠军> jqGrid的UI< /标题>
    <链接相对=样式类型=文本/ CSSHREF =HTTP://$c$c.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css/>
    <链接相对=样式类型=文本/ CSSHREF =HTTP://www.trirand.com/blog/jqgrid/themes/ui.jqgrid.css/>

    &所述;脚本的src =的http://$c$c.jquery.com/jquery-1.10.1.min.js>&所述; /脚本>

    <脚本类型=文/ JavaScript的SRC =HTTP://www.trirand.com/blog/jqgrid/js/jquery-ui-custom.min.js'>< / SCRIPT>
    <脚本类型=文/ JavaScript的SRC =HTTP://www.trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js'>< / SCRIPT>
    <脚本类型=文/ JavaScript的SRC =HTTP://www.trirand.com/blog/jqgrid/js/jquery.jqGrid.js'>< / SCRIPT>

    <脚本>
    $(文件)。就绪(函数(){
        $(#list_records)。jqGrid的({
            网址:getGridData.php,//我想改变这是同一个页面,从数据库中调用数据的,我必须使用的数据:varname的?
            数据类型:JSON,
            MTYPE:GET,//我应该改变这种数据类型:本地,如果我想改变的网址是什么?
            colNames:用户ID,用户名,名,姓],
            colModel:
                {名字:因维,对齐:右},
                {名字:invdate},
                {名字:量},
                {名字:税}
            ]
            寻呼机:#perpage
            的rowNum:10,
            rowList:[10,20],
            sortname:因维,
            排序顺序:ASC
            高度:自动,
            viewrecords:真正的,
            gridview的:真正的,
            标题: 
        });
    });
    < / SCRIPT>
< /头>

<身体GT;
<表ID =list_records>< TR>< TD>< / TD>< / TR>< /表>
< D​​IV ID =perpage>< / DIV>
< /身体GT;
< / HTML>
 

和我有下面的PHP MySQL的code:

 < PHP
$康恩=的mysql_connect(localhost的根,)或死亡(连接错误:mysql_error());
mysql_select_db(jqGrid的)或死亡(错误连接到数据库。);

$页= $ _GET ['页'];
$上限= $ _GET ['行'];
$ SIDX = $ _GET ['SIDX'];
$ SORD = $ _GET [SORD'];

如果(!$ SIDX)$ SIDX = 1;

$结果= mysql_query(SELECT COUNT(*)作为计数从invheader);
$行= mysql_fetch_array($结果,MYSQL_ASSOC);

$数= $行['数'];
如果($计数大于0和放大器;&安培; $限制大于0){
    $ TOTAL_PAGES = CEIL($计数/ $限制);
} 其他 {
    $ TOTAL_PAGES = 0;
}
如果($页> $ TOTAL_PAGES)$页= $ TOTAL_PAGES;
$启动= $限制* ​​$页 -  $限制;
如果($开始℃,)$开始= 0;

$ SQL =SELECT * FROM invheader ORDER BY $ SIDX $ SORD LIMIT $开始,$限制;
$结果= mysql_query($的SQL)或死亡(无法执行查询。mysql_error());

$ i = 0;
而($行= mysql_fetch_array($结果,MYSQL_ASSOC)){
    $ responce->行[$ i] ['身份证'] = $行['因维'];
    $ responce->行[$ i] ['细胞'] =阵列($行['invdate'],$行['量'],$行['税'],$行['总']) ;
    $ I ++;
}
回声json_en code($性反应);
?>
 

这是完全正常的。不过,我不希望使用GetMethod的。我想把这一切在一个PHP页面。能有人给我我如何能做到这一点preferably使用数组的样品code。在此先感谢!

更新

我现在已经设法找到了关于我如何PHP的查询结果转换为JavaScript的按照此处给出的指令的 http://www.dyn-web.com/tutorials/php-js/json/array.php

我现在的问题是,我无法使用转换变量到jqGrid的。我想有相同的结果,它是此网页。 http://trirand.com/blog上/jqgrid/jqgrid.html#t95 ,唯一不同的是我用,而不是建立一个静态的数据转换PHP成果转化javascropt。

下面是我的脚本code:

  VAR MYDATA =<?PHP的回声json_en code($ dataitems)取代;
jQuery的(#list4)。jqGrid的({
    数据:MYDATA,
    数据类型:本地,
    高度:250,
    colNames:['INV无,日期,客户,金额,税法,合计,注意]
    colModel:
        {名字:'身份证',索引:'身份证',宽度:60,sorttype:INT},
        {名字:invdate,索引:invdate',宽度:90,sorttype:日期},
        {名称:名称,索引:'名',宽度:100},
        {名称:'量',索引:'量',宽度:80,对齐:正确的,sorttype:浮动},
        {名称:'税',索引:'税',宽度:80,对齐:正确的,sorttype:浮动},
        {名称:'总',索引:'总',宽度:80,对齐:正确的,sorttype:浮动},
        {名字:'注意',索引:'注意',宽度:150,排序:假}
    ]
    多选:真正的,
    标题:操控阵列数据
});
 

解决方案

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

  $数据=阵列();
而($行= mysql_fetch_assoc($结果))$数据[] = $行;
回声'<脚本> VAR数据='。 json_en code($的数据)。 ';< / SCRIPT>';
 

在这一点上,你将有一个包含所有数据的JSON对象。这将使一个较长的页面加载,但会做你的要求没有Ajax

而我在这, mysql_职能是去precated 。 Copnsider切换到的mysqli _

I'm new to jqgrid. I did alot of research on how to call the data from the database without using GET method without success.

I have below HTML code:

<!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>

And I have the php mysql code below:

<?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);
?>

This is fully working. However, I don't want to use the getmethod. I want to put it all in a php page. Can someone give me a sample code on how can I achieve this preferably using an array. Thanks in advance!

UPDATE

I have now managed to find out on how I can convert php query results to javascript by following the instruction given here http://www.dyn-web.com/tutorials/php-js/json/array.php

My problem now is I'm unable to use the converted variable into jqgrid. I wanted to have the same result that is on this page http://trirand.com/blog/jqgrid/jqgrid.html#t95, only difference is I used converted php results into javascropt instead of creating a static data.

Below is my script code:

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"
});

解决方案

While this isn't terribly efficient you can do something like this

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

At this point you will have a JSON object that contains all your data. This will make for a longer page load but will do what you asked without AJAX

While I'm at it, mysql_ functions are deprecated. Copnsider switching to mysqli_

这篇关于调用服务器上的数据,而不使用在jqGrid的GET方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆