0

So second and hopefully last question for today!

Hello to everyone, I'm a beginner in PHP and I'm making a reservation app.

Assume a restaurant with tables of 2 or 4 seats and reservations up to 8 people at a time.

The problem arises in the reservation of 8.

I'm making a function that fills an array with the available tables then picks one randomly, assigning it to the reservation.

For the res of 8, I need two 4 seat tables.

The first problem I encountered was duplication of tables but i somewhat solved that using the following code:

if ($tableCount['total'] > 0) {
echo "Available tables: " . $tableCount['total'] . " " . "<br><br>";
//query for displaying available tables   
$sql2 = "SELECT * FROM tables WHERE status_id = 1 AND chair_count = 4 AND (section_id = 3 OR section_id = 4)";
$result = mysqli_query($conn, $sql2);
//array filling with values of 4 chair tables
while ($row = mysqli_fetch_assoc($result)) {
    array_push($availableTables, $row['tableid']);
    $resTable1 = $availableTables[array_rand($availableTables)];
}
//array cleaning from previous fill to avoid assigning the same table id twice
$availableTables = array();
$sql3 = "SELECT * FROM tables WHERE status_id = 1 AND chair_count = 4 AND (section_id = 3 OR section_id = 4)";
$result = mysqli_query($conn, $sql3);
while ($row = mysqli_fetch_assoc($result)) {
    array_push($availableTables, $row['tableid']);
    if (($key = array_search($resTable1, $availableTables)) !== false) {
        unset($availableTables[$key]);
    }
    $resTable2 = $availableTables[array_rand($availableTables)];
}

the problem arises in the case that it hits a duplicate value in the second loop. while it does replace the value I get an error of unidentified index in line x.

I tried finding a reroll function of sorts or creating one but failed. any ideas?

  • Check it with the [isset](http://php.net/manual/en/function.isset.php) function: `if isset($availableTables[$key])) unset($availableTables[$key]);` (And good for you, for not ignoring Notice-level messages). – alanlittle Apr 19 '17 at 13:47
  • im just testing the code u provided thanks! – Filippos Georgiou Apr 19 '17 at 13:54
  • also what is a notice level message? :P – Filippos Georgiou Apr 19 '17 at 13:54
  • PHP error messages have [different levels](http://php.net/manual/en/errorfunc.configuration.php#ini.error-reporting). Notice-level is the lowest, and some people just ignore them, but it's generally good practice to take care of them. – alanlittle Apr 19 '17 at 14:59

0 Answers0