Link to home
Start Free TrialLog in
Avatar of Shruti Upari
Shruti UpariFlag for India

asked on

How to display the json data within array?

My code for extracting the data is as follows:

$query = mysqli_query($con,"SELECT `item_id` ,  `shop_id` ,  `item_list_date`   FROM  `item_list`  WHERE `yb_id` = '$yb_id'");
//echo "SELECT `item_id` ,  `shop_id` ,  `item_list_date`   FROM  `item_list`  WHERE `yb_id` = '$yb_id'";
if(mysqli_affected_rows($con)>0)
{
while ($row2 = mysqli_fetch_object($query))
{
    $data1[] = $row2;    
}
//}

//"SELECT  `item_list_details`.`item_id` ,  `item_name` ,  `item_qty` ,  `item_unit` ,  `item_detail` ,  `item_flag` FROM  `item_list_details` LEFT JOIN  `item_list` ON  `item_list`.`item_id` =  `item_list_details`.`item_id` WHERE  `item_list`.`item_id` =50 AND  `yb_id` =  '70380KTXDY'";


$query = mysqli_query($con,"SELECT  `item_list_details`.`item_id` ,  `item_name` ,  `item_qty` ,  `item_unit` ,  `item_detail` ,  `item_flag` FROM  `item_list_details` LEFT JOIN  `item_list` ON  `item_list`.`item_id` =  `item_list_details`.`item_id` WHERE  `item_list`.`yb_id` =  '$yb_id'");
//echo "SELECT  `item_list_details`.`item_id` ,  `item_name` ,  `item_qty` ,  `item_unit` ,  `item_detail` ,  `item_flag` FROM  `item_list_details` LEFT JOIN  `item_list` ON  `item_list`.`item_id` =  `item_list_details`.`item_id` WHERE  `item_list`.`yb_id` = '$yb_id'";
if(mysqli_affected_rows($con)>0)
{
while ($row2 = mysqli_fetch_object($query))
{
    $data2[] = $row2;    

}
}
$data['errorcode'] = 45000;
$data['list_details'] = $data1;
$data['item_list_details'] = $data2;
$data4['details'] = $data;
//echo json_encode($data3);
//}
}
else
   {
       $data['errorcode'] = 45001;
       
//       $data['details']=$data5;
    //echo'You entered username or password is incorrect';
   }
   echo json_encode($data4);

Open in new window


Output that I'm getting is:

{"details":{"errorcode":45000,"list_details":[{"item_id":"49","shop_id":"1","item_list_date":"2017-12-20"},{"item_id":"67","shop_id":"1","item_list_date":"2017-12-04"},{"item_id":"92","shop_id":"1","item_list_date":"2017-12-12"},{"item_id":"93","shop_id":"1","item_list_date":"2017-12-12"}],"item_list_details":[{"item_id":"92","item_name":"chocolates","item_qty":"1","item_unit":"gms","item_detail":"1 packet","item_flag":"0"},{"item_id":"92","item_name":"qwerty","item_qty":"2","item_unit":"2","item_detail":"rice flour","item_flag":"0"},{"item_id":"93","item_name":"chocolates","item_qty":"1","item_unit":"gms","item_detail":"1 packet","item_flag":"0"},{"item_id":"93","item_name":"qwerty","item_qty":"2","item_unit":"2","item_detail":"rice flour","item_flag":"0"}]}}

Open in new window


But I want the output in the below format, so how do I do that?
{
"Item_Response" :
{ 
“errorcode” : “45000”,
“message” : “success”,
“lists_Details” :
 [{
   “List_id” : “”,
   “ShopId” : “”,
   “ ShopName” : “”,
   “Date” : “”,
   “Seen_status” : “”,
   “List_details” : 
[
{
  “Item_name” : “rice”,
  “Qty” : “1kg”,
  “Details” : “”
},
{
  “Item_name” : “rice”,
  “Qty” : “1kg”,
  “Details” : “”
}]},
{
   “List_id” : “”,
   “ShopId” : “”,
   “ ShopName” : “”,
   “Date” : “”,
   “Seen_status” : “”,
   “List_details” : 
[
{
  “Item_name” : “rice”,
  “Qty” : “1kg”,
  “Details” : “”
},
{
  “Item_name” : “rice”,
  “Qty” : “1kg”,
  “Details” : “”
}]}]}}

Open in new window

SOLUTION
Avatar of Shahzad Fateh Ali
Shahzad Fateh Ali
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shruti Upari

ASKER

Thank You for your help. I did it as per the instructions given by you all and have finally executed the code in the format which I was getting stuck at. It was all your support that helped me solve this issue.
Hi Shruti,

If you received a solution then you should close your question.  Here are the steps on how to close your question:  http://support.experts-exchange.com/customer/en/portal/articles/2527982-how-do-i-close-my-question-?b_id=44

Let me know if this works or not.