I'm trying to generate a JSON API using PHP to be used as remote server interface for my Android application database.
I managed to generate JSON like this:
{
products: [
{
product_name: "Samsung",
product_category: "phones",
shop_name: "Gadget Store",
user_id: "1",
price: "1999",
date: "2015-04-05",
time: "11:14:44"
},
{
product_name: "Samsung",
product_category: "phones",
shop_name: "IT Store",
user_id: "1",
price: "1899",
date: "2015-04-01",
time: "13:00:00"
},
{
product_name: "Motorola",
product_category: "phones",
shop_name: "IT Store",
user_id: "1",
price: "1499",
date: "2015-04-02",
time: "10:31:29"
}
]
}
But I guess I need a nested JSON which is something like this:
{
products: [
{
product_name: "Samsung",
product_category: "phones",
shops: [
{
shop_name: "Gadget Store",
user_id: "1",
price: "1999",
date: "2015-04-05",
time: "11:14:44"
},
{
shop_name: "IT Store",
user_id: "1",
price: "1899",
date: "2015-04-01",
time: "13:00:00"
}
],
},
{
product_name: "Motorola",
product_category: "phones",
shops: [
shop_name: "IT Store",,
user_id: "1",
price: "199",
date: "2015-04-02",,
time: "10:31:29"
],
}
]
}
How can I achive this result?
The sql query is from 3 different table. Below is my current code:
class productDB
{
public $product_name = "";
public $product_category = "";
public $shop_name = "";
public $user_id = "";
public $price;
public $date = "";
public $time = "";
function __construct($product_name, $product_category, $shop_name, $user_id, $price, $date, $time)
{
$this->product_name = $product_name;
$this->product_category = $product_category;
$this->shop_name = $shop_name;
$this->user_id = $user_id;
$this->price = $price;
$this->date = $date;
$this->time = $time;
}
class Shop
{
public $shop_name = "";
public $user_id = "";
public $price;
public $date = "";
public $time = "";
function __construct($shop_name, $user_id, $price, $date, $time)
{
$this->shop_name = $shop_name;
$this->user_id = $user_id;
$this->price = $price;
$this->date = $date;
$this->time = $time;
}
}
class product
{
public $product_name = "";
public $product_category = "";
public $shop = "";
function __construct($product_name, $product_category, $shop_name, $user_id, $price, $date, $time)
{
$this->product_name = $product_name;
$this->product_category = $product_category;
$this->shop = new Shop($shop_name, $user_id, $price, $date, $time);
}
}
$query = "SELECT a.product_name, a.product_category,
b.shop_name,
c.user_user_id, c.price, c.date, c.time
FROM price c, item a, shop b
WHERE c.product_product_id = a.product_id AND c.shop_shop_id = b.shop_id";
$product_array = array();
if ($result = $dbc->query($query)) {
while ($obj = $result->fetch_object()) {
$temp_product[] = new ProductDB(
$obj->product_name,
$obj->product_category,
$obj->shop_name,
$obj->user_id,
$obj->price,
$obj->date,
$obj->time);
$product_array = $temp_product;
}
//Give a name to the array
$array_name = 'products';
$product_array = (array($array_name=>$product_array));
$product_object = json_encode($product_array);
echo $product_object;