javascript - I have nested while loop php mysql. how to encode it to valid JSON

<?php
// header("Access-Control-Allow-Origin: *");
// header("Content-Type: application/json; charset=UTF-8");
require_once ('connection.php');
try {
$selectCatagory = 'select Catagory_id, Catagory_name, Catagory_image from district_shop.catagory';
$prepareCatagory = $conn -> prepare($selectCatagory);
$prepareCatagory ->execute(array());
while ($fetchCatagory = $prepareCatagory ->fetch(PDO::FETCH_ASSOC)) {
$Catagory = $fetchCatagory['Catagory_id'];
$selectSubCatagory = 'select * from district_shop.subcatagory where Catagory_id= :Cat_id';
$prepareSubCatagory = $conn -> prepare($selectSubCatagory);
$prepareSubCatagory -> execute(array(
'Cat_id' => $Catagory
));
while ($fetchSubCatagory = $prepareSubCatagory -> fetch(PDO::FETCH_ASSOC)) {
//print_r($fetchSubCatagory);
echo json_encode($fetchSubCatagory);
// It doesnot encoded to valid JSON
}
}
} catch (PDOException $th) {
$th-> errMessage();
}
?>
// The outpuut invalid JSON file is as :-
[{"Subcatogory_id":"1","Subcatagory_name":"Crasher Material","Subcatagory_description":"Crasher Material","Active_Subcatagory":"Active","Catagory_id":"1"},{"Subcatogory_id":"2","Subcatagory_name":"Plumbing Items","Subcatagory_description":"Plumbing Items","Active_Subcatagory":"Active","Catagory_id":"1"},{"Subcatogory_id":"3","Subcatagory_name":"Iron Material","Subcatagory_description":"Iron Material","Active_Subcatagory":"Active","Catagory_id":"1"},{"Subcatogory_id":"4","Subcatagory_name":"Cementing","Subcatagory_description":"Cement Material","Active_Subcatagory":"Active","Catagory_id":"1"},{"Subcatogory_id":"5","Subcatagory_name":"Marble & Tiles","Subcatagory_description":"Marble & Tiles","Active_Subcatagory":"Active","Catagory_id":"1"},{"Subcatogory_id":"6","Subcatagory_name":"Electric Material","Subcatagory_description":"Electric Material","Active_Subcatagory":"Active","Catagory_id":"1"}][{"Subcatogory_id":"7","Subcatagory_name":"Sweets ","Subcatagory_description":"Sweets ","Active_Subcatagory":"Active","Catagory_id":"2"},{"Subcatogory_id":"8","Subcatagory_name":"Salted(Namkeen)","Subcatagory_description":"Salted(Namkeen)","Active_Subcatagory":"Active","Catagory_id":"2"},{"Subcatogory_id":"9","Subcatagory_name":"Cold and Beverages","Subcatagory_description":"Cold and Beverages","Active_Subcatagory":"Active","Catagory_id":"2"},{"Subcatogory_id":"10","Subcatagory_name":"Food and Snacks","Subcatagory_description":"Food and Snacks","Active_Subcatagory":"Active","Catagory_id":"2"}]
Sir, I am unable to create its valid json as i have two tables in mySQL named catagory and subcatagory. so nested while loop is used to extract subcatagory by using catagory. The output is encoded in json but it was no a valid JSON.
Answer
Solution:
The problem is you're echoing multiple JSONs without nesting them properly. Instead of this:
[subcat1, subcat2, subcat3][subcat4, subcat5, subcat6]
(invalid)
Your output should look like be one of these
[subcat1, subcat2, subcat3, subcat4, subcat5, subcat6]
(valid flat array)
[[subcat1, subcat2, subcat3], [subcat4, subcat5, subcat6]]
(valid nested arrays)
In your code, you should harvest responses into an array, which youecho json_encode()
in the end.
...
$output = [];
while ($fetchCatagory ...) {
...
while ($fetchSubCatagory ...) {
// for nested array
$output[] = $fetchSubCatagory;
// for flat array
$output = array_merge($output, $fetchSubCatagory);
}
}
echo json_encode($output);
That should do the trick :-)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: installation failed, reverting ./composer.json and ./composer.lock to their original content.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.