I am using PHP and some JSON arrays to show data from a table. Following code is like following image :
My prob: I used the following code for offering some packages to my user on an android app. in my food_category table, Ids 6-7-8 are drink and meal and dessert. How can I show complete food and drink and meal and dessert in many arrays like the following image that show all information on 1 array? Now my code separates ids and names and ... in some arrays. Can u help me, guys?
Food table:
Code:
public function package_recommender($restaurant_id, $count, $money)
{
$response = array();
$response['success'] = true;
$response['message'] = "success";
$connection = null;
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$connection = mysqli_connect("localhost", "root", "", PackageRecommender::DATABASE_NAME);
$sqlQuery1 = "SELECT * FROM food where NOT foodCategory_id=6 And NOT foodCategory_id=7 And NOT foodCategory_id=8 And restaurant_id='$restaurant_id' And stock>='$count' And isActive = 1;";
$result1 = $connection->query($sqlQuery1);
$sqlQuery2 = "SELECT * FROM food WHERE foodCategory_id=7 And restaurant_id = '$restaurant_id' And stock>'$count' And isActive = 1 order by updateDate desc;";
$result2 = $connection->query($sqlQuery2);
$sqlQuery3 = "SELECT * FROM food WHERE foodCategory_id=8 And restaurant_id = '$restaurant_id' And isActive = 1 order by updateDate desc;";
$result3 = $connection->query($sqlQuery3);
$sqlQuery4 = "SELECT * FROM food WHERE foodCategory_id=6 And restaurant_id = '$restaurant_id' And isActive = 1 order by updateDate desc;";
$result4 = $connection->query($sqlQuery4);
$desertArray = $result2->fetch_assoc();
$drinkArray = $result3->fetch_assoc();
$mealArray = $result4->fetch_assoc();
$packagesArray = array();
if ($result1->num_rows > 0) {
$foodArray = $result1->fetch_assoc();
foreach ($foodArray as $key => $foodValue) {
$package = array();
$package['food'] = $foodValue;
if (isset($mealArray[$key])) {
$package['meal'] = $mealArray[$key];
}
if (isset($drinkArray[$key])) {
$package['drink'] = $drinkArray[$key];
}
if (isset($desertArray[$key])) {
$package['desert'] = $desertArray[$key];
}
array_push($packagesArray, $package);
}
$response["package"] = $packagesArray;
}
echo json_encode($response);
} catch (mysqli_sql_exception $e) {
$response['success'] = false;
$response['message'] = "error 101";
die(json_encode($response));
}
}


