Am Trying to create a JSON array by leveraging info three database tables.
After leveraging info and solution found here source
I came to realize that i have to use foreign keys for references and also use join queries.
The three tables is created as follows each with their reference keys
Create table categorys(id int primary key auto_increment, category_name varchar(30), buyer_userid int(30));
Create table product(id int primary key auto_increment, product_name varchar(30), buyer_userid int(30),category_id int,
foreign key (category_id) references categorys(id));
Create table sales(items_id int primary key auto_increment, item_name varchar(30),quantity varchar(30), buyer_userid int(30),
product_id int, foreign key (product_id) references product(id));
Here is my expected JSON Format...
[
{"id":"1","cat_name":"Provision","catbuy_userid":"100",
"products_info":[{"productid":"2","product_name":"Malt","buyer_userid":"100",
"sales_Info":[{"items_id":"1","item_name":"malt","buyer_userid":"100","quantity":"72"}]
}]},
{"id":"2","cat_name":"Cosmetics","catbuy_userid":"200",
"products_info":[{"productid":"3","product_name":"soapy","buyer_userid":"200",
"sales_Info":[{"items_id":"2","item_name":"cream","buyer_userid":"200","quantity":"83"}]
}]}
]
Here is the my coding so far
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "mydb"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$res_arr = array();
$query = '
SELECT
c.id,
c.category_name,
c.buyer_userid,
p.id,
p.product_name,
p.buyer_userid,
p.category_id,
s.items_id,
s.item_name,
s.quantity
FROM `categorys` c
INNER JOIN `product` p ON c.id=p.category_id
INNER JOIN `sales` s ON p.id=s.product_id
ORDER BY c.id';
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$res_arr[] = $row;
}
echo json_encode($res_arr);
exit;
?>
Here is my json output
[{"0":"1","id":"1","1":"Provisions","category_name":"Provisions","2":"100","buyer_userid":"100","3":"4","4":"Malt","product_name":"Malt","5":"100","6":"1","category_id":"1","7":"4","items_id":"4","8":"malt","item_name":"malt","9":"93","quantity":"93"},
{"0":"2","id":"2","1":"Cosmetics","category_name":"Cosmetics","2":"200","buyer_userid":"200","3":"3","4":"soapy","product_name":"soapy","5":"200","6":"2","category_id":"2","7":"3","items_id":"3","8":"cream","item_name":"cream","9":"83","quantity":"83"}
]
Please how do I loop to get result as per json format above.
Updated Section
Error warning Sample They are so many error warnings when i run the code. below is just few of them
Warning: Use of undefined constant products_info - assumed 'products_info' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\api\test.php on line 51
Warning: Use of undefined constant product_name - assumed 'product_name' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\api\test.php on line 51