Reputation:
Hi i have written an android app which interacts with php to get data from the database.
I need the json to return the data back in this format.
[
{
"outlet_id" :"1",
"outlet_desc" :"BigBazaar",
"outlet_loc" :"Jayanagar4thBlock",
"outlet_image" :"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FBigBazaar.png",
"recommended_products": [
{
"item_id":"3",
"item_desc":"Dettol",
"item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FDettol.png"
}
]
},
{
"outlet_id":"2",
"outlet_desc":"FoodWorld",
"outlet_loc":"Basavanagudi",
"outlet_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FFoodWorld.png","
recommended_products":[
{
"item_id":"3",
"item_desc":"Dettol",
"item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FDettol.png"
},
{
"item_id":"3",
"item_desc":"Colgate",
"item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FColgate.png"
}
]
},
{
"outlet_id":"5",
"outlet_desc":"Total",
"outlet_loc":"MurgeshPalaya",
"outlet_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FTotal.png",
"recommended_products":[
{
"item_id":"3",
"item_desc":"Dettol",
"item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FDettol.png"
},
{
"item_id":"3",
"item_desc":"Colgate",
"item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FColgate.png"
}
]
}
]
But right now it is giving in the following format.
[
[
{
"outlet_id": "1",
"outlet_name": "Big Bazaar",
"lat": "12.9285690",
"lng": "77.5831100",
"outlet_image": "images/BigBazaar.png",
"outlet_location": "Jayanagar 4th Block"
}
],
[
{
"outlet_id": "2",
"outlet_name": "Food World",
"lat": "12.9392350",
"lng": "77.5780680",
"outlet_image": "images/FoodWorld.png",
"outlet_location": "Basavanagudi"
}
],
[
{
"outlet_id": "5",
"outlet_name": "Total",
"lat": "12.9589340",
"lng": "77.6571610",
"outlet_image": "images/Total.png",
"outlet_location": "Murugeshpalya"
}
],
[
{
"outlet_id": "6",
"outlet_name": "Big Bazaar",
"lat": "12.9324963",
"lng": "77.6228463",
"outlet_image": "images/BigBazaar.png",
"outlet_location": "Koramangala"
}
],
[
{
"outlet_id": "7",
"outlet_name": "Food World",
"lat": "12.9785055",
"lng": "77.6379353",
"outlet_image": "images/FoodWorld.png",
"outlet_location": "Indira Nagar"
}
]
]
This is my php code.
<?php
error_reporting(0);
//$url = $_GET['url'];
//$mR = $_GET['mRequest'];
//$mOid = $_GET['mOutletId'];
//$mloc = $_GET['mLocation'];
//connect to the db
$user = "root";
$pswd = "";
$db = "recommendations_db";
$host = "localhost";
$conn = mysql_connect($host, $user, $pswd);
mysql_select_db($db);
//if($mR == 'outlets' && $mloc = 'all'){
$query = "SELECT * FROM outlets";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
while($row = mysql_fetch_assoc($result))
{
$output[] = array($row);
}
print( json_encode($output) );
?>
How can i modify the above php code to return in the 1st format. Please help as i am not so good in php.
Table Structure.
Outlets Table.
It contains the following fields.
outlet_id outlet_name lat lng outlet_image outlet_location
Recommendations table.
It contains the following fields.
item_id outlet_id
Items table.
It contains the following fields.
item_id item_name item_image item_price category_id
Upvotes: 0
Views: 157
Reputation: 28419
Ok so it's the missing sub-array. You want to create a JSON string, not a object, from a multidimensional associative array.
There are faster ways to select these rows but this is easy to follow.
$query = "SELECT * FROM outlets";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
$output=array(); # don't leave it undefined
while($row = mysql_fetch_assoc($result))
{
# guessing this query
$query2 = "SELECT * FROM recommended_products WHERE outlet_id=".$result['outlet_id'];
# now we create the sub array recommended_products
$row['recommended_products']=array();
$result2 = mysql_query($query2) or die("Unable to verify user because : " . mysql_error());
while($row2 = mysql_fetch_assoc($result2)){
$row['recommended_products'][]=$row2;
}
# then add the modified row to the output
$output[] = $row;
}
print( json_encode($output) );
Also, mysql_ is depreciated. Probably no genuine need to rush but it's worth at least looking at converting to mysqli or PDO.
Upvotes: 1