user6362696 user6362696 - 6 months ago 13
SQL Question

PHP: Loop looping through result set

I am having a huge issue looping through results, These two queries work hand in hand to check if a restaurant is open today. My problem is i have restaurants, id 1-5(more in the future). But the loop seems to only get restaurant id 5. I have read many posts on here and it seems like i am doing the right thing. But i cannot seem to loop to get the other restaurant id's.

I am blocked now, newbie who is very open to any suggestions or advise.

$sel = "SELECT Rest_Details.Resturant_ID,Delivery_Pcode.Pcode,Delivery_Pcode.Restaurant_ID
FROM Rest_Details INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID=Rest_Details.Resturant_ID
WHERE Delivery_Pcode.Pcode LIKE'$searchP'";
$res = $dbc->query($sel);

if (!$res) {
echo "invalid query '" . mysqli_error($dbc) . "\n";
}
$i=1;
while ($row_res = $res->fetch_array()) {
$rest_ = $row_res['Resturant_ID'];
$i++;
}


date_default_timezone_set("Europe/London");

$daynum = jddayofweek(unixtojd());

$query = "SELECT *
FROM Opening_hrs WHERE
Restaurant_ID = $rest_
AND Day_of_week = $daynum";

$run_qu = $dbc->query($query);

if ($run_qu->num_rows > 0) {
while ($row_qu = $run_qu->fetch_assoc()) {
$message = "open" . $row_qu["Open_time"] . "</br>";
}
} else {
$message = $message . "close" . $row_qu["Closing_time"] . "</br>";
}

Answer

You could either output whatever you want to within your loop or build-up an output string because the value of $rest_ will always be the last value in the loop and i don't think that's what you want... Again you are doing the same with $message. And I am willing to bet that this is what you want to do:

        <?php 
        date_default_timezone_set("Europe/London");

        $sel = "SELECT Rest_Details.Resturant_ID,Delivery_Pcode.Pcode,Delivery_Pcode.Restaurant_ID 
                 FROM Rest_Details INNER JOIN Delivery_Pcode
                 ON Delivery_Pcode.Restaurant_ID=Rest_Details.Resturant_ID
                 WHERE Delivery_Pcode.Pcode LIKE'$searchP'";
        $res = $dbc->query($sel);

        if (!$res) {
            echo "invalid query '" . mysqli_error($dbc) . "\n";
        }

        $i=1;

        while ($row_res = $res->fetch_array()) {
            $rest_ = $row_res['Resturant_ID'];
            $i++;       // <== YOU DON'T NEED THIS VARIABLE....

            // GET THE DATES WITHIN THE LOOP...     
            $daynum = jddayofweek(unixtojd());      
            $query  = "SELECT *
                 FROM Opening_hrs WHERE
                 Restaurant_ID = $rest_
                 AND Day_of_week = $daynum";

            $run_qu = $dbc->query($query);

            if ($run_qu->num_rows > 0) {
                while ($row_qu = $run_qu->fetch_assoc()) {
                    $message = "open" . $row_qu["Open_time"] . "</br>";
                }
            } else {
                $message = $message . "close" . $row_qu["Closing_time"] . "</br>";
            }       

        }
Comments