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
Not populating JSON data
07/07/2010
18:21
Avatar
Wakie
Member
Members
Forum Posts: 4
Member Since:
07/07/2010
sp_UserOfflineSmall Offline

Hi all,

I've been trying to debug this for ages with no success, hopefully someone can point me in the right direction.

I'm trying to replicate one of the examples in the documentation but can't get it to work. Here's my client-code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DT.....tional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="includes/classes/jqgrid/js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="includes/classes/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>

<script src="includes/classes/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>

<link type="text/css" href="/sandbox/css/dark-hive/jquery-ui-1.8.custom.css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" media="screen" href="includes/classes/jqgrid/css/ui.jqgrid.css" />
 
<style>
html, body {
 margin: 0;
 padding: 0;
 font-size: 75%;
}
</style>
<script type="text/javascript">
jQuery().ready(function (){
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>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<table id="list2"></table>
<div id="pager2"></div>

</body>
</html>

JSON data looks like this:

{"page":1,"total":100,"records":"1","rows":[{"id":"200","cell":["200","111","test","664.3","144","900","yay"]}]}

Any tips would be appreciated.

Thanks,

Wakie.

07/07/2010
18:33
Avatar
churd
Member
Members
Forum Posts: 11
Member Since:
23/06/2010
sp_UserOfflineSmall Offline

I had a super hard time getting JSON data to populate... it ended up being the way that it was connecting to the database.

Assuming that your files are setup properly on the server (I made sure mine were by manually populating the data without any JSON or XML calls)... what I did was replace the "dbconfig.php" call with my own connection document, and then replace the "$db = " in my server.php file with my own connection item.

Any chance you could paste your entire server.php file? (excluding your login info, of course). 

07/07/2010
19:08
Avatar
Wakie
Member
Members
Forum Posts: 4
Member Since:
07/07/2010
sp_UserOfflineSmall Offline

Hi churd,

It's essentially a hacked up version of the demo PHP script, but here it is anyway:

<?php
$page = 1;//$_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('localhost', 'root', '')
or die("Connection Error: " . mysql_error());

mysql_select_db('fone_site') 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 = 100;//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";
$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);
?>

07/07/2010
19:24
Avatar
churd
Member
Members
Forum Posts: 11
Member Since:
23/06/2010
sp_UserOfflineSmall Offline

Not sure I'll be able to help you, but my issue was in the $db portion...

Are you sure that the following allows you proper access to your database?

$db = mysql_connect('localhost', 'root', '')

07/07/2010
19:26
Avatar
churd
Member
Members
Forum Posts: 11
Member Since:
23/06/2010
sp_UserOfflineSmall Offline

Is it pulling up a blank table, or no table at all?

07/07/2010
19:51
Avatar
Wakie
Member
Members
Forum Posts: 4
Member Since:
07/07/2010
sp_UserOfflineSmall Offline

Database connectivity is fine, it returns the JSON output in my first post. I'm pretty concerned that even the provided examples don't work...

07/07/2010
20:13
Avatar
Reg
Calgary, Canada
Member
Members
Forum Posts: 92
Member Since:
06/06/2008
sp_UserOfflineSmall Offline

Hello,

This may be a red herring, but I got burned on something similar to this today, until I found the following note in the jQuery site:

 Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes.

I notice that not all of the data returned in your json string is enclosed in double quotes. You might wnat to try changing that to see if it makes any difference (even the numbers). 

HTH

Reg

08/07/2010
05:25
Avatar
Wakie
Member
Members
Forum Posts: 4
Member Since:
07/07/2010
sp_UserOfflineSmall Offline

Hi Reg, thanks for your reply.

Unfortunately your tip made no difference - my JSON now looks like this:

{"page":"1","total":"100","records":"1","rows":[{"id":"200","cell":["200","111","test","664.3","144","900","yay"]}]}

Regards

Wakie

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
63 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