-1

私は予約システムを開発しています。顧客は最大 3 席まで予約できます。顧客が 1 席のみを予約した場合、これは他の 2 席の値が null に設定されていることを意味します。フォームを送信するときに、顧客が同じ席を二重に予約しようとしていないことを確認します。顧客が 1 シートを購入すると、両方のシートが null に等しいため、エラーが発生します。これを解決するにはどうすればよいですか? ありがとう

if($seat1==$seat2 || $seat2==$seat3 || $seat1==$seat3){
echo("You're trying to book the same seat numerous times, please go back.");
die();
}
4

2 に答える 2

1

Use arrays. They can have as many values as you like, so you don't have a limit of 3, although you could enforce it still. And while you're at it, you can use a class as well. Just for the fun of it. ;)

$mrA = new User("GolezTrol");
$mrB = new User("user1715417");

try {

    $booking = new Booking();
    $booking->bookSeat($mrA, 'A7');
    $booking->bookSeat($mrA, 'A8');
    // $booking->bookSeat($mrB, 'A8'); // Double booking
    $booking->bookSeat($mrB, 'A9');
    $booking->bookSeat($mrA, 'B7');
    // $booking->bookSeat($mrA, 'B8'); // Only 3 seats allowed per user
    $booking->bookSeat($mrB, 'B9');
    // $booking->bookSeat($mrB, 'C7'); // No Seat Left

    var_dump($booking->bookedSeats); // Output Booking

} catch ( Exception $e ) {
    echo $e->getMessage(), PHP_EOL;
}

Output

array
  'GolezTrol' => 
    array
      0 => string 'A7' (length=2)
      1 => string 'A8' (length=2)
      2 => string 'B7' (length=2)
  'user1715417' => 
    array
      0 => string 'A9' (length=2)
      1 => string 'B9' (length=2)

Class Used

class User {
    public $id;

    function __construct($id) {
        $this->id = $id;
    }

    function __toString() {
        return $this->id;
    }
}

class Booking {
    public $bookedSeats = array();
    public $seatUsed = array();
    private $maxSeat = 5;
    private $maxBooking = 3;

    public function bookSeat(User $user, $seat) {
        if (count($this->seatUsed) >= $this->maxSeat) {
            throw new Exception('No Seat Left');
        }

        if (in_array($seat, $this->seatUsed)) {
            throw new Exception('Double booking');
        }

        if (array_key_exists($user->id, $this->bookedSeats)) {
            if (count($this->bookedSeats[$user->id]) >= $this->maxBooking) {
                throw new Exception('Only 3 seats allowed');
            }

            $this->bookedSeats[$user->id][] = $seat;
            $this->seatUsed[] = $seat;
        } else {
            $this->bookedSeats[$user->id] = array();
            $this->bookedSeats[$user->id][] = $seat;
            $this->seatUsed[] = $seat;
        }
    }
}

You can see how easy it would be to put the limit at 5 or 8 seats, without having to modify anything but a number, while your current check is already complex and will become twice as complex if you grow from 3 to 5.

于 2012-10-12T22:09:57.797 に答える
0

これが明白な答えです、私は他の誰かを派手で派手にさせます:)

if ( $seat1 === $seat2 || $seat1 === $seat3 || ($seat2 !== null && $seat2 === $seat3) )
于 2012-10-12T22:06:12.883 に答える