No idea why this no longer array and no comma between {},{}
foreach ($query->result_array() as $row) {
header("Content-type: application/json");
echo (json_encode($row,JSON_PRETTY_PRINT));
}
And how to alert json data in jquery? Before this I use for loop in js to loop it but for some reason I have to loop in server side now.
Edited
//because i want to replace $row['url'] to get_thumb();
foreach($query->result_array() as $row){
$row['url'] = $this->vz_image->get_thumb($row['url'],'n');
header("Content-type: application/json");
echo (json_encode($row,JSON_PRETTY_PRINT));
}
Then I got this result, but I have no idea how to alert this json data in jquery. Any help?
{
"id": "44",
"url": "assets\/img\/uploaded_photos\/thumbs\/1358786330_n.jpg",
"created_time": "1358786330",
"photo_id": "170",
"tag_id": "44",
"name": "Lan Yao Yeng",
"num_photo": "3"
}{
"id": "44",
"url": "assets\/img\/uploaded_photos\/thumbs\/1358711532_n.jpg",
"created_time": "1358711532",
"photo_id": "169",
"tag_id": "44",
"name": "Lan Yao Yeng",
"num_photo": "3"
}
If you want to pass the entire array to the client, just encode the array itself, not each elements.
function map($row){
if (isset($row['url']))
$row['url'] = $this->vz_image->get_thumb($row['url'],'n');
return $row;
}
header("Content-type: application/json");
echo json_encode(array_map("map", $query->result_array()),JSON_PRETTY_PRINT);
If you use this in a class, you may have to change array_map("map", $query->result_array())
to array_map(array($this, "map"), $query->result_array())
.
It isn't an array because you aren't giving it an array.
You're echoing a number of objects one after the other; not an array of objects.