0

I am using PHP and some JSON arrays to show data from a table. Following code is like following image :

enter image description here

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?

enter image description here

Food table:

enter image description here

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));
    }
}
4
  • I have the same problem :) Commented Aug 20, 2018 at 16:20
  • what is the criteria of grouping? Commented Aug 20, 2018 at 17:05
  • @Suchitkumar Restaurant_id & Count of people & User money. Check question again I edited for showing food table Commented Aug 20, 2018 at 17:18
  • @Suchitkumar Please check question's edit. Can you guide me? Commented Aug 21, 2018 at 7:49

1 Answer 1

1

Well, I'm gonna take a huge leap here and assume you have the same number of food, meals, drinks and deserts, and that you only want to display them as a combination ina a package without any particular order or logic.

So looking at your code, you nowhave 4 arrays:

$foodArray = array();
$desertArray = array();
$drinkArray = array();
$mealArray = array();

And if you want to have a simple combination of them as a package, this would be the easieast way, fisrt you declare a packages array:

$packagesArray = array();

And then just iterate through one of your array (like food), make a key - value pair, check for other arrays in one foreach loop and add a package:

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;

Now, like I said, I have no idea if the packages are in some way related with the food, drinks and meals. This is just a simple example.

Sign up to request clarification or add additional context in comments.

10 Comments

Thx my friend. But I didn't understand. Can you explain more?
You go through each element of foodArray one by one. This foreach condition $foodArray as $key => $foodValue makes sure you get the index of each element. So when you're on 2nd index of foodArray for example (try print_f($foodArray); etc after each sql returned result to see indexes), you check if other arrays have any value at the same index , if they do, you add them in a temporary $package array. In the end you push that array into packagesArray (the json_encode maybe shows it as an object if these are simple arrays). At each step of that foreach loop $package array is reset.
Hey, I have some progress on my coding. But I saw another problem. I edited my question. please check again and help me if you have time. Thanks a lot.
How did you get that array in the first image? Can you show me a few rows of data in the database?
Ok, I'll probably get annoying here, but can you also show me a data dump or json array of foodArray or desertArray. Any other array than the packaesArray.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.