Jump to content

Blank JGrid table with Json+php+mysql


alphasil

Recommended Posts

Hi

 

I have another problem with my code

I have records in my database and i want to put them in Jgrid but no records are show.

This is my php

<?php
error_reporting(0);
require_once 'database_connection.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
if(!$sidx) $sidx =1;
// connect to the database
$result = mysql_query("SELECT COUNT(*) AS count FROM utilizador");
$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 * FROM utilizador 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[idutilizador];
    $responce->rows[$i]['cell']=array($row[idutilizador],$row[nome],$row[utilizador],$row[telefone],$row[email]);
    $i++;
}        
echo json_encode($responce);
?>

And this is the Jgrid

<html>
<head>
<title>jQGrid example</title>
<!-- Load CSS--><br />
<link rel="stylesheet" href="css/ui.jqgrid.css" type="text/css" media="all" />
<!-- For this theme, download your own from link above, and place it at css folder -->
<link rel="stylesheet" href="css/jquery-ui-1.9.2.custom.css" type="text/css" media="all" />
<!-- Load Javascript -->
<script src="js/jquery_1.5.2.js" type="text/javascript"></script>
<script src="js/jquery-ui-1.8.1.custom.min.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-pt.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
</head>
<body>
<table id="datagrid"></table>
<div id="navGrid"></div>
<p><script language="javascript">
jQuery("#datagrid").jqGrid({
   	url:'example.php',
    datatype: "json",
    colNames:['Idutilizador','Nome', 'Utilizador', 'Telefone','Email'],
    colModel:[
		{name:'idutilizador',index:'idutilizador', width:55,editable:false,editoptions:{readonly:true,size:10}},
		{name:'nome',index:'nome', width:80,editable:true,editoptions:{size:10}},
		{name:'idutilizador',index:'idutilizador', width:90,editable:true,editoptions:{size:25}},
		{name:'telefone',index:'telefone', width:60, align:"right",editable:true,editoptions:{size:10}},
		{name:'email',index:'email', width:60, align:"right",editable:true,editoptions:{size:10}}
	],
	rowNum:10,
	rowList:[10,15,20,25,30,35,40],
	pager: '#navGrid',
	sortname: 'idutilizador',
	sortorder: "asc",
	height: 500,
	width:900,
	viewrecords: true,
	caption:"Atividades Registadas"
});
jQuery("#datagrid").jqGrid('navGrid','#navGrid',{edit:true,add:true,del:true});
</script>
</body>
</html>

Anything wrong with the code?

 

Regards

Link to comment
Share on other sites

Hi

 

Thank you

 

my server.php is now

<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
require_once 'database_connection.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
if(!$sidx) $sidx =1;
// connect to the database
$result = mysql_query("SELECT COUNT(*) AS count FROM utilizador");
$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 * FROM utilizador 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[idutilizador];
    $responce->rows[$i]['cell']=array($row[idutilizador],$row[nome],$row[utilizador],$row[telefone],$row[email]);
    $i++;
}        
echo json_encode($responce);
?> 

I have put the error reporting and when i run only server.php i have this

 

Notice: Undefined index: page in C:\xampp\htdocs\login\server.php on line 5
Notice: Undefined index: rows in C:\xampp\htdocs\login\server.php on line 6
Notice: Undefined index: sidx in C:\xampp\htdocs\login\server.php on line 7
Notice: Undefined index: sord in C:\xampp\htdocs\login\server.php on line 8
Warning: Division by zero in C:\xampp\htdocs\login\server.php on line 16
Couldn t execute query.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

But i'm following this tutorial

 

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

Link to comment
Share on other sites

Following the tutorial is no excuse for not addressing the errors you are clearly seeing.

 

You are going to get those errors if you blindly go trying to capture input values that don't exist yet. You need to do some checking to see if you have any input yet or if this is the initial execution of this script. Also - where is this apparent "input" coming from? Another script that should precede this one? Is that script actually sending input via a GET or should you be receiving a POST array?

 

If you don't understand this then you need to do some reading on how data is handled by a browser and delivered to a php script. It would be a good learning experience regardless.

Link to comment
Share on other sites

Ok

Almost everything fixed but now i having this problem

The Jgrid gives me this url

Request URL:http://localhost/login/server.php?_search=false&rows=20&page=1&sidx=&sord=asc

And nothing is show

 

but if i put this url manually i have records

http://localhost/login/server.php?_search=false&page=1&rows=3&sidx=1&sord=asc

So how can i change the Jgrid url?

 

regards

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.