I am trying the following code:
$order = array(); $imageURL = array(); $name = array();
while($row = mysql_fetch_array($mysql->result)) {
$order[] = $row["order"];
$imageURL[] = $row["imageURL"];
$name[] = $row["name"];
}
$res = array($order, $imageURL,$name);
return json_encode($res);
But it is not outputting in json format, any ideas?
Output:
[["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"],["previews\/en-1-1.gif","previews\/en-1-2.gif","previews\/en-1-3.gif","previews\/en-1-4.gif","previews\/en-1-5.gif","previews\/en-1-6.gif","previews\/en-1-7.gif","previews\/en-1-8.gif","previews\/en-1-9.gif","previews\/en-1-10.gif","previews\/en-1-11.gif","previews\/en-1-12.gif","previews\/en-1-13.gif","previews\/en-1-14.gif","previews\/en-1-15.gif","previews\/en-1-16.gif","previews\/en-1-17.gif","previews\/en-1-18.gif","previews\/en-1-19.gif","previews\/en-1-20.gif","previews\/en-1-21.gif","previews\/en-1-22.gif","previews\/en-1-23.gif","previews\/en-1-24.gif","previews\/en-1-25.gif","previews\/en-1-26.gif","previews\/en-1-27.gif","previews\/en-1-28.gif","previews\/en-1-29.gif","previews\/en-1-30.gif"],["Helasd you?","Where sasaddsdam?","Weasdd!","Tasasdther","AtsaddsaBeach","Cheasd Hotel","At the Hotel","aaaaaaaaat?","At the Market","Aasdt's","Mesadasd th","Shdsdsssg","sssss","On aaaa","Do you work or study?","aaaaaaa","At tadstation","aaaaaae Gym","How doasdto\u2026?","Planning a Trip","At adsk","At the asdurant","At the Inads00e9","My Taaaog","A Meetiaaaaay adher","Tourist sdsadn Centre","Saaaaing","Aaaaaa Match","Lookaaasd","At tasda"]]
I think you're just after a better format to work with in your output, because you've currently set it up with three arrays and you'll have to reference each record's properties across these arrays by a numeric key.
I've reconstructed an example of what your original database output would look like:
$array = json_decode($json, true);
$row = array();
list($orders, $imageURLS, $names) = $array;
foreach($orders as $key => $val) {
$row[] = array(
'order' => $val,
'imageURL' => $imageURLS[$key],
'name' => $names[$key]
);
}
So in your code, you should try this:
$output = array();
while($row = mysql_fetch_array($mysql->result)) {
$output[] = $row;
}
return json_encode($output);
And you'll get a much easier data structure to work with. Example:
[
{
"order": "1",
"imageURL": "previews\/en-1-1.gif",
"name": "Helasd you?"
},
{
"order": "2",
"imageURL": "previews\/en-1-2.gif",
"name": "Where sasaddsdam?"
}
]
Judging by the comments to your original question, it looks like you're looking for :
$main_array = array() ;
while($row = mysql_fetch_array($mysql->result)) {
$res = array($row["order"], $row["imageURL"],$row["name"]);
$main_array[] = $res;
}
return json_encode($main_array);
The output is correct. A json array, containing three json arrays. But I guess you want each element to be a separate array/object, containing order
, imageURL
, name
. If that is correct, try something like:
$result = array();
while($row = mysql_fetch_array($mysql->result)) {
$result[] = array("order"=>$row["order"], "imageURL" => $row["imageURL"],"name"=> $row["name"])
}
$res = array($order, $imageURL, $name);
return json_encode($result);