Forum
21:00
03/05/2012
Hello people.
I'm using PHP to load a JSON to JQGRID but the table is coming empty. I have printed the PHP result and I can't find the error.
My data (created via PHP):
My PHP code:
$conexao = new Conexao;
$code = mysql_escape_string($_GET['code']);
$page = mysql_escape_string($_GET['page']);
$limit = mysql_escape_string($_GET['rows']);
$sidx = mysql_escape_string($_GET['sidx']);
$sord = mysql_escape_string($_GET['sord']);
if( !$sidx ) { $sidx = 1; }
$contador = Reuniao::total_listar($code, true, 'Ativo'); // select data count in mysql table
if( $contador > 0 ) {
$total_pages = ceil($contador/$limit);
} else $total_pages = 0;
if($page > $total_pages) { $page = $total_pages; }
$start = $limit * $page – $limit;
$linha = Reuniao::listar($code, false, NULL, $sidx, $sord, $start, $limit); // select data in mysql table
$saida = '{ "page": "' . $page . '", "total": "' . $total_pages . '", "records": "' . $contador . '", "rows": [';
$count = 1;
foreach ($linha as $l):
if( $count == count($linha) ) {
$saida .= '{ "id": "' . $l['cod_reuniao'] . '", "cell": ["teste", "teste", "teste", "teste", "teste", "teste", "teste", "teste"] }';
} else {
$saida .= '{ "id": "' . $l['cod_reuniao'] . '", "cell": ["teste", "teste", "teste", "teste", "teste", "teste", "teste", "teste"] },';
}
$count++;
endforeach;
$saida .= ']}';
echo $saida;
Can anyone help me to see what is wrong here?
I can't see the definition of Reuniao::listar, but there is a very high chance that your code contains SQL injection vulnerabilities, especially around the page, limit and sord parameters. You cannot simply mysql_escape some of these parameters and remain protected.
Second, you are building the JSON object by hand, and improperly escaping data (quotes within your values) that would lead to invalid JSON syntax. Instead of:
foreach ($linha as $l):
if( $count == count($linha) ) {
$saida .= '{ "id": "' . $l['cod_reuniao'] . '", "cell": ["teste", "teste", "teste", "teste", "teste", "teste", "teste", "teste"] }';
} else {
$saida .= '{ "id": "' . $l['cod_reuniao'] . '", "cell": ["teste", "teste", "teste", "teste", "teste", "teste", "teste", "teste"] },';
}
You should try using json_encode to do all of the PHP array to JSON object conversion:
$saida = array();
foreach ($linha as $l):
if( $count == count($linha) ) {
$saida[] = array('id' => $l['code_reuniao'], 'cell' => array('teste', 'teste', 'teste, 'teste', etc....));
} else {
$saida[] = array('id' => $l['cod_reunaio'], 'cell' => array('teste', 'teste', 'testes', etc...));
}
endforeach;
echo json_encode($saida);
Most Users Ever Online: 715
Currently Online:
26 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.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66