0

So I'm making this system where you can reserve a seat but when I'm doing a check if the seat is already reserved it screws up(Means that I'm too bad with loops). I have 3 test users in my database and it echoes the seats as many times as there are users. And it even gets the reserved seats correctly coloured.

$sql = "SELECT * FROM users";
$result = mysql_query($sql);
$list = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $reserved = $row["seat"];
    $list[] = $reserved;
}

echo '<div id="top-section">';
for($i = 0; $i < 44; $i++) {
    $seatCount++;
    foreach($list as $obj) {
        if($seatCount == $obj) {
            echo '<div id="seat_'.$seatCount.'" class="seat reserved">'.$seatCount.'</div>';
        }
        else {
            echo '<div id="seat_'.$seatCount.'" class="seat available" onclick="selectSeat('.$seatCount.');">'.$seatCount.'</div>';
        }
    }
}

echo '</div>';

And the result is this: http://i.gyazo.com/d10e787cea7028b46c716ac41766456a.png (I have three divs of seats and only done the loop on the top part so don't mind the 45 - 68 since they are correct). How do I make it so it only posts the seats once?

  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Oct 03 '14 at 21:24
  • Why do you need the `$seatCount` variable, isn't it the same as `$i`? – Barmar Oct 03 '14 at 21:30
  • I need it for later in the code since like I said I have three divs of seats and I use it on the 2 later divs too. –  Oct 04 '14 at 07:08

5 Answers5

0

Your foreach ($list as $obj) is inside the loop. You have three reservations, so it's printing the seat three times. You need to move this outside the loop.

danmullen
  • 2,556
  • 3
  • 20
  • 28
0

Use in_array():

for ($i = 0; $i < 44; $i++) {
    if (in_array($i, $list)) {
        echo '<div id="seat_'.$seatCount.'" class="seat reserved">'.$seatCount.'</div>';
    }
    else {
        echo '<div id="seat_'.$seatCount.'" class="seat available" onclick="selectSeat('.$seatCount.');">'.$seatCount.'</div>';
    }
}

Even better would be to make $list use the reserved seats as keys, not values:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $reserved = $row["seat"];
    $list[$reserved] = true;
}

Then your second loop could use:

if (isset($list[$i]))
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You could make something like this:

$sql = "SELECT * FROM users";
$result = mysql_query($sql);
$list = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $reserved = $row["seat"];
    $list[$reserved] = 1;
}

echo '<div id="top-section">';
for($i = 0; $i < 44; $i++) {
    $seatCount++;
    if(isset($list[$seatCount]) AND $list[$seatCount] == 1) {
        echo '<div id="seat_'.$seatCount.'" class="seat reserved">'.$seatCount.'</div>';
    }
    else {
        echo '<div id="seat_'.$seatCount.'" class="seat available" onclick="selectSeat('.$seatCount.');">'.$seatCount.'</div>';
    }
}

echo '</div>';
Jeroen de Jong
  • 337
  • 1
  • 6
0

You don't need to have that inner foreach statement, if I think I know what you're going for.

There's two things wrong with your code, though.

Firstly, this statement is a little redundant

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $reserved = $row["seat"];
    $list[] = $reserved;
}

Change it to this, you know, for simplicity:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $list[] = $row["seat"];
}

Then, the loop can look a little something like this (which uses in_array instead of another loop):

for($i = 0; $i < 44; $i++) {
    $seatCount++;

    if(in_array($seatCount, $list)) {
        echo '<div id="seat_'.$seatCount.'" class="seat reserved">'.$seatCount.'</div>';
    } else {
        echo '<div id="seat_'.$seatCount.'" class="seat available" onclick="selectSeat('.$seatCount.');">'.$seatCount.'</div>';
    }
}

Then, this way, the $seatCount variable will be looked for in the user seats and if it is, it will show the reserved seat and if not will just show a selectable seat.

helllomatt
  • 1,331
  • 8
  • 16
0

You should use in_array() and remove one foreach loop.

<?php
$list = array(1,2,5);
echo '<div id="top-section">';
for($i = 0; $i < 44; $i++) {
        if(in_array($i,$list)) {
            echo '<div id="seat_'.($i+1).'" class="seat reserved">'.($i+1).'</div>';
        }
        else {
            echo '<div id="seat_'.($i+1).'" class="seat available" onclick="selectSeat('.($i+1).');">'.($i+1).'</div>';
        }
}

echo '</div>';
?>

Demo: https://eval.in/201702

ThatMSG
  • 1,456
  • 1
  • 16
  • 27