how to use while() with json?
i have a ajax that needs to return some values, but if i use a while()
to get all the result from my db, the return is "nothing".
js:
$.post("ajax/file.ajax.php",
{
cache: false
},
function(data){
alert(data.region);
},'json'
);
php:
while(!$res->EOF)
{
header('Content-Type: application/json');
$return_data = array('value'=>''.$res->fields['VALUE'].'','region'=>''.$res->fields['REGION'].'开发者_StackOverflow中文版');
echo json_encode($return_data);
$res->MoveNext();
}
so, how can i use the while with json ?
The easiest solution would be to create an array and then array.push
the row you are generating onto the end of it.
For example:
PHP
$array = array();
while(!$res->EOF) {
$dataFromDB = array('value'=>''.$res->fields['VALUE'].'','region'=>''.$res->fields['REGION'].'');
$array[] = $dataFromDB; //or array_push($array, $dataFromDB);
$res->MoveNext();
}
header('Content-Type: application/json');
echo json_encode($array);
JS:
$.post("ajax/file.ajax.php",
{
cache: false
},
function(data){
for(n in data) {
alert(data[n].region);
}
},'json'
);
Then output the json encoded array to finish.
You should also move your header out of the while loop and put it just before you echo the new array.
Try the following...
PHP:
$return_data = array();
while(!$res->EOF){
$return_data[] = array(
'value' => $res->fields['VALUE'],
'region' => $res->fields['REGION']
);
$res->MoveNext();
}
header('Content-Type: application/json');
echo json_encode($return_data);
jQuery:
$.post("ajax/file.ajax.php", {cache: false}, function(data){
for(var i in data){
alert(data[i].region);
}
},'json');
$.post("ajax/file.ajax.php",
{
cache: false
},
function(data){
//alert(data.region);
if(data.region.length>0){
for(var i=0;i<data.region.length;i++) {
alert(data.region[i]);
}
}
},'json'
);
header('Content-Type: application/json');
$return_data = array();
while(!$res->EOF)
{
$return_data[] = array('value'=>''.$res->fields['VALUE'].'','region'=>''.$res->fields['REGION'].'');
$res->MoveNext();
}
echo json_encode($return_data);
So you only echo the data once, and it's an array with all of the data. In the JS you'll just have to loop through them.
Try building up the array and then do a single json_encode of the contents. The PHP code you listed is sending sequential fragments of json that are not valid as a whole.
$data = array();
while(!$res->EOF)
{
$data[] = array('value'=>''.$res->fields['VALUE'].'','region'=>''.$res->fields['REGION'].'');
$res->MoveNext();
}
header('Content-Type: application/json');
echo json_encode($data);
A couple things to look at:
- Don't use
header()
in a loop. Headers can't be sent after sending content, which you do with theecho
. Send it once before sending any content (json). You aren't sending valid JSON. Since you echo each result, your output looks like this:
{"value":"foo1", "region":"bar1"} {"value":"foo2", "region":"bar2"}
JSON arrays need to be within brackets (http://www.json.org/).
[ {"value":"foo1", "region":"bar1"}, {"value":"foo2", "region":"bar2"} ]
So, form your query results array, then send encoded content:
<?php
$return_data = array();
while (!$res->EOF) {
$return_data[] = array(
'value' => '' . $res->fields['VALUE'] . '',
'region' => '' . $res->fields['REGION'] . ''
);
$res->MoveNext();
}
header('Content-Type: application/json');
echo json_encode($return_data);
?>
精彩评论