Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
jqgrid help - php json fill table
18/03/2014
13:38
Avatar
ggcristian
New Member
Members
Forum Posts: 2
Member Since:
18/03/2014
sp_UserOfflineSmall Offline

Hi,

I have some troubles getting the grid populated with the data retrieved from the database.

my HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DT.....t;&gt;
<html xmlns="http://www.w3.org/1999/xhtml&q.....t;&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>tabele</title>
<link rel="stylesheet" type="text/css" media="screen" href="jquery-ui.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script src="jquery.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-ro.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script src="js/ui.multiselect.js" type="text/javascript"></script>

<script type="text/javascript">
 
 jQuery(document).ready(function () {
            jQuery("#list2").jqGrid({
                url: 'server.php',
                datatype: 'json',
                mtype: 'GET',                
                colNames: ['ID', 'Data plecarii', 'Oras plecare', 'Oras destinatie', 'Username'],
                colModel: [{
                    name: 'ID',
                    index: '1',
                    width: 150,
                    align: 'center'
 
                }, {
                    name: 'dataplecare',
                    index: '2',
                    width: 150,
                    align: 'left'
                    
                }, {
                    name: 'orasplecare',
                    index: '3',
                    width: 150,
                    align: 'left'
                  
                }, {
                    name: 'orasdestinatie',
                    index: '4',
                    width: 200,
                    align: 'left'
                    
                },{
                    name: 'username',
                    index: '4',
                    width: 200,
                    align: 'left'
                }
                ],
 
                pager: jQuery('#pager2'),
                rowNum: 5,
                rowList: [5, 10, 20, 50],
                sortorder: "asc",
                viewrecords: true,
                caption: 'Marfa',
                pgbuttons: true,
                loadtext: 'Incarcare date...',
                loadui: 'block',
                height: 'auto'
            });

  jQuery("#list2").navGrid('#pager2', {

                edit: true, add: true, del: true, search: true
  });

        });
</script>
</head>
<body>
<table id="list2"></table>
<div id="pager2"></div>
</body>
</html>

my server.php

<?php
$dbhost = 'localhost';
$dbuser= 'root';
$dbpassword = 'dbpass';
$database = 'dbname';
$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
if(!$sidx) $sidx =1;
// connect to the database
$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
mysql_select_db($database) or die("Error conecting to db.");
$result = mysql_query("SELECT COUNT(*) AS count FROM marfa");
$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; // do not put $limit*($page - 1)
$SQL = "SELECT id, dataplecare, orasplecare, orasdestinatie, username FROM marfa ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
$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[id];
  $responce->rows[$i]['cell']=array($row[id],$row[dataplecare],$row[orasplecare],$row[orasdestinatie],$row[username]);
  $i++;
 }
echo json_encode($responce);
?>

The problem is the table is rendered empty. If anybody has some suggestions please show me the way...

18/03/2014
13:57
Avatar
ggcristian
New Member
Members
Forum Posts: 2
Member Since:
18/03/2014
sp_UserOfflineSmall Offline

I have also tried the demo using the folowing files:

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DT.....t;&gt;
<html xmlns="http://www.w3.org/1999/xhtml&q.....t;&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>tabele</title>
<link rel="stylesheet" type="text/css" media="screen" href="jquery-ui.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script src="jquery.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-ro.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script src="js/ui.multiselect.js" type="text/javascript"></script>
</head>
<body>
<table id="list2"></table>
<div id="pager2"></div>
<script>
jQuery("#list2").jqGrid({
    url:'server.php?q=2',
    datatype: "json",
    colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],
    colModel:[
    {name:'id',index:'id', width:55},
    {name:'invdate',index:'invdate', width:90},
    {name:'name',index:'name asc, invdate', width:100},
    {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}
            ],
    rowNum:10,
    rowList:[10,20,30],
    pager: '#pager2',
    sortname: 'id',
    viewrecords: true,
    sortorder: "desc",
    caption:"JSON Example"
     });
     jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false});
</script>
</body>
</html>

server.php

<?php
$dbhost = 'localhost';
$dbuser= 'root';
$dbpassword = 'dbpass';
$database = 'griddemo';
$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
if(!$sidx) $sidx =1; // connect to the database
$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
mysql_select_db($database) or die("Error conecting to db.");
$result = mysql_query("SELECT COUNT(*) AS count FROM invheader a, clients b WHERE a.client_id=b.client_id");
$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; // do not put $limit*($page - 1)
$SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
$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[id];
$responce->rows[$i]['cell']=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
$i++; }
echo json_encode($responce);
?>

The result is the same: the grid is rendered empty.

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
24 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information