1

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>";
}
10
  • 1
    well, you loop through your restaurants, overwrite $rest_ each time with the new id-value. Maybe you want to have the 'big' loop spanning til the end of your code? Commented May 27, 2016 at 23:28
  • 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. What do you want to output? Can you tell us? That would help a lot... Commented May 27, 2016 at 23:34
  • Better still, is to get all information from one query, not two. That will be much more efficient, even though some restaurant data will be repeated. Commented May 27, 2016 at 23:35
  • What was wrong with my answer in your previous question stackoverflow.com/questions/37488634/… ? Commented May 27, 2016 at 23:36
  • 1
    @Barmar It's indeed very strange to ask the same question twice instead of asking about details in the asnwers of the first question.... Commented May 27, 2016 at 23:42

2 Answers 2

1

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>";
            }       

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

1 Comment

i am astonished i was so close but couldn't put two and two together, Thank you so much, for this and your time
1

I think this is what you are trying to do.

// $searchP should be checked to prevent SQL injection. 
$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_IDW
        WHERE Delivery_Pcode.Pcode LIKE '$searchP'";
$res = $dbc->query($sel);

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

// set these once as they don't change
date_default_timezone_set("Europe/London");
$daynum = jddayofweek(unixtojd());

// $i=1; - not required, never used
// loop over the original results
while ($row_res = $res->fetch_array()) {
    $rest_ = $row_res['Resturant_ID'];
    //$i++; not used

    // check for a match 
    $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) {
        // at least one match
        while ($row_qu = $run_qu->fetch_assoc()) {
            $message = "open" . $row_qu["Open_time"] . "<br />";
            $message .= "close" . $row_qu["Closing_time"] . "<br />";
        }
    } else {
        // no matches
        $message = "No results for <i>$daynum</i>.";
    }
}

It should be possible to get the details in a single query, but I would need to see your SQL tables for that (and you did not ask for that too :]).

Also, it is <br> or <br />, not </br>.

2 Comments

right answer, but you should also point out, that the final if-else with getting 'closing_time' won't work, as there is no $row_qu (maximum an old one).
@Jeff Thanks, Was checking for other issues and missed that, fixed now.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.