Jump to content

Recommended Posts

Thank you for taking the time to read this.

I have an excercise where I need to build a small part of  seat reservation system for a movie theatre.

Seats are assigned to guests who want to reserve tickets. As input I receive a quantity of visitors and as output I need to return a array with the reserved seatnumbers. Everyone needs to sit close to each other as possible.

I have to think about making a script where I look into the list of seats where there’s room for the quantity of visitors.

 

There are only seat numbers (not rows) so I only have to make sure that the seatnumbers are assigned to the guests as close as possible.

I need to fit the number of visitors in between the seats that are already taken. If there are more seats available then I have to assign the lowest seatnumber. If there is no room for the visitors to sit to each other then I have to look elsewhere, where to fit them.

E.g: if there is no room for 7 visitors to each other then I have to find a place where there can be 6 and the 7th will sit elsewhere. If it’s not possible I’ll return null instead of the array.

 

To start off: what is the best php function to look for any room into an array and make a new array from the results?

 

E.g: seatnumbers 1-4 are available and 5-8 are taken. My script will find room for my 4 visitors and will assign seatnumbers 1-4 and present this as a new array.

This is my code:

 

<?php
function DBConnect(){
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "movie-theatre";
    $connection = new mysqli($servername, $username, $password, $dbname);

    if ($connection->connect_error){
        die("Connection failed: " . $connection->connect_error);
    }
    else{
        return $connection;    
    }    
}

function giveSeatNumbers($visitors){
    $seats = array();
    echo "<ol>";
    for($x = 1; $x < $visitors+1; $x++){
        array_push($seats,$x);
    }
    echo "</ol>";            
    /*
        within the list, try to fit the visitors in the seats array,
        each visitor must get one seat, if available, starting with the lowest value.
    */    
}

function getSeats(){
    DBConnect();
    $sql = "SELECT seats.* FROM seats
    WHERE seats.Available = 1;
    ";
    $result = DBConnect()->query($sql);
    return $result;
}

$seats = getSeats();
echo "Seats from database:";
for($x=0; $x<5; $x++){
    while($seat = $seats->fetch_assoc()) {
        echo $seat['seatNumber'];
    }
}
//This is where I define the number of visitors
$quantityVisitors = 10;
giveSeatNumbers($quantityVisitors);
?>
<p>
    Quantity of tickets ordered: <?php echo $quantityVisitors;?>
</p>
<p>
    The following seats will be assigned to you:
</p>
Edited by Barand
remove text from code box

You need to take seat location into account. If you have, say,

 

  • 10 seats in 2 rows of 5
  • seats 1-3 are booked
  • 3 people require seats
1*  2*  3*  4   5
  
6   7   8   9   10

then allocating seats 4,5,10 would be a better solution than 4,5,6

 

similar post that may help

http://forums.phpfreaks.com/topic/284542-allocated-seating-layout-help/?do=findComment&comment=1461309

Hey Barand,

   Seems from the post there are no rows, so you only have to deal with gaps.

 

@jiros1:

 

Seems like these are your requirements:

 

 

  1. There are only seat numbers (not rows) so I only have to make sure that the seatnumbers are assigned to the guests as close as possible.
  2. ... fit the number of visitors in between the seats that are already taken. If there are more seats available then I have to assign the lowest seatnumber.
  3. If there is no room for the visitors to sit to each other then I have to look elsewhere, where to fit them.  (E.g: if there is no room for 7 visitors to each other then I have to find a place where there can be 6 and the 7th will sit elsewhere.)
  4. If it’s not possible I’ll return null instead of the array.

 

I can't think of any specific array functions that will help you with this other than array_merge for building the initial master seating array from the list of reserved seats and the entire theater.  What does occur to me is that a data structure that stores seat openings from first to last might be helpful.  If you generated something like this:

 

 

 

$gaps[] = array('start' => 1, 'end' => 4, 'count' => 4);

 

Then you could traverse that looking for blocks of seats that are >= the size you need.

 

Obviously your function would need to traverse the master array once it's loaded with reservations and generate the $gaps array.

  • Like 1

Thanks for the quick replies!

@Barand, I don't need the rows, the seats are only defined as  seat numbers, but I understand why you would think that.

 

@Gizmola, your "$gaps[]" approach looks very interesting and might just be what I need.

I get a vague idea of your theory but I don't how to actually make this.

Can you please elaborate how I can implement your idea?

$gaps[] = array('start' => 1, 'end' => 4, 'count' => 4);

 

Then you could traverse that looking for blocks of seats that are >= the size you need.

 

Obviously your function would need to traverse the master array once it's loaded with reservations and generate the $gaps array.

Hey Jiros1,

  Well assuming you have a master array loaded with the reservations (start with a simple array of the size of the theater, initialized with some known value like '--empty--', array_merge with your reservation array.

 

Make sure that your array is "one based" in that you don't have a zeroth element, but rather, that each element 1-n matches the appropriate seat number.

 

Initialize your $gaps array as empty:  $gaps = array();

 

You then simply need to foreach() through the master array using a boolean and counter variable.  Just off the top of my head:

 

 

 
$counter = 0;
$start = 0;
$end = 0;
 
foreach ($master as $index => $seat) {
    if ($seat !== '--empty--') {
        // Close out and add a gap if appropriate
        if ($counter > 0) {
             $end = $start + $counter -1;
             $gaps[] = array('start' => $start, 'end' => $end, 'count' => $counter);
            // reset counters
             $start = $counter = $end = 0;
      
        }
    } else {
        // There is an open seat, add to range variables
        if ($start == 0) {
            $start = $index;
        }
        $counter++; 
    }

 

 

Assuming you have an array like this, try and write a routine that will traverse it using foreach, and looking for a counter that is

 

If you go through and can't find a spot big enough, then you could try different strategies for going through it again and looking for close matches, and then using your rules to break up the reservation into multiple reservations.  

  • 2 weeks later...

@Gizmola, based on your example I have made a script that runs through the seats and makes gaps along the way.

 

It checks if the provided quantity of visitors fit in the gaps. My question is how do I make it so that it executes only the first gap?

 

current output:

3 visitors

it fits123Seat: 1(gap size: 5)
it fits111213Seat: 11(gap size: 5)

 

It starts at the first available seat from there it increments by one until it reaches the quantity of visitors. This is one gap.

 

This is my code:

functions.php

<?phpfunction connectDB(){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "movie-theatre";
$connection = new mysqli($servername, $username, $password, $dbname);


if ($connection->connect_error){
     die("Connection failed: " . $connection->connect_error);
}
else{
  return $connection; 
} 
}


function fetchSeats(){
connectDB();
$sql = "SELECT seats.* FROM seats 
";
$result = connectDB()->query($sql);
return $result;
}


function suggestSeats($startingSeatNumber,$quantity) {
  for($x=0; $x < $quantity; $x++) { 
    echo $startingSeatNumber;
    $startingSeatNumber++; 
  } 
}
?>

index.php

<?php// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
include "functions.php";
connectDB();
//$seatsAvailable = [];
$seatReservation = [];
$gap = 0;
$visitors = 3;
// $start = 0;
$seatNumber = ""; 
$seats = fetchSeats();


echo "<h2>".$visitors." visitors</h2>";
while($seat = $seats->fetch_assoc()){
  //seat available
  if($seat['seatAvailable']){
  //when there's no gap set the current seatnumber and increment the gap size
  //so we know how much capacity there is for the visitors.
  if($gap == 0){
  $seatNumber = $seat['seatNumber'];
  }
  $gap++;
  }
  //seat taken
  else{
  //If there's a gap
  if($gap > 0){ 
  //If the quantity of visitors fit in the gap
  if($visitors <= $gap){
  echo "it fits";
  suggestSeats($seatNumber,$visitors);
  echo "<strong>Seat: ".$seatNumber. "(gap size: ".$gap. ")</strong></br>";
  }
  // $start = 1;
  $gap = 0;
  $seatNumber = "";
  }
  }
}

if($gap > 0) {
  if($visitors <= $gap){
    echo "it fits";
    suggestSeats($seatNumber,$visitors);
    echo "<strong>Seat: ".$seatNumber. "(gap size: ".$gap. ")</strong></br>";
  }
  //echo "<strong>Seat: ".$seatNumber. "(gap size: ".$gap. ")</strong>";
}
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.