Allocated Seating Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

superlative
Forum Newbie
Posts: 10
Joined: Wed Dec 04, 2013 10:24 pm

Allocated Seating Help

Post by superlative »

Hi,

I am building an online ticketing system for a theatre where there is allocated seating.
There are 5 rows, each with 25 seats.

E1 to E2
D1 to D25
C1 to C25
B1 to B25
A1 to A25

E row is at the front of the cinema.
A is at the back.

At the moment I have got as far as to check a database for booked seats, and then have two arrays - one with the seats that are booked, and one with all seats there are in the cinema. One array subtracts duplicates from the other, so that the seats we are left with are seats that have not been booked. I have the list arranged in A-Z order.

So at the moment the online ticketing sells from back left (being A1) to the right and then on to the next row and so on.

I have two issues, and I really need someone who has done alot of coding to explain the best way to do this.

Issue 1
I need to have the seating allocation start from the centre of the front row (E) and work outwards.
For example if someone books two seats and they are the first booking they may get E12/E15.
Then next booking may be for four people and it would get four seats from the middle available maybe E8, E9, E10, E11.

Issue 2
The next problem is that say if you had all but 2 seats booked in the first row. Seats E24 and E25 are left.
Someone books seats online for 3 people, the computer would give them E24, E25 and D12 for example.
I need the script to check to make sure the number of tickets booked all have seat numbers starting with the same row number. The maximum tickets that can be booked online is 5 in any one transaction. So I need it to look for up to 5 seats in a row, there cant be a booking with split rows.

Again, any help appreciated.

Please see below my current code:

Code: Select all

$ArraySeats = array('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A11', 'A12', 'A13', 'A14', 'A15', 'A16', 'A17', 'A18', 'A19', 'A20', 'A21', 'A22', 'A23', 'A24', 'A25','B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11', 'B12', 'B13', 'B14', 'B15', 'B16', 'B17', 'B18', 'B19', 'B20', 'B21', 'B22', 'B23', 'B24', 'B25','C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11', 'C12', 'C13', 'C14', 'C15', 'C16', 'C17', 'C18', 'C19', 'C20', 'C21', 'C22', 'C23', 'C24', 'C25','D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'D10', 'D11', 'D12', 'D13', 'D14', 'D15', 'D16', 'D17', 'D18', 'D19', 'D20', 'D21', 'D22', 'D23', 'D24', 'D25','E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8', 'E9', 'E10', 'E11', 'E12', 'E13', 'E14', 'E15', 'E16', 'E17', 'E18', 'E19', 'E20', 'E21', 'E22', 'E23', 'E24', 'E25');

	$result = mysql_query("SELECT seat_number FROM ticket_sales where seat_number <> '' AND void IS NULL AND session_id = $session_id ORDER BY seat_number ASC") 
	or die(mysql_error());  


	// Set initial ArraySeatsTaken as blank so that it can never be null.
	$ArraySeatsTaken[] = '';

	// keeps getting the next row until there are no more to get
	while($row = mysql_fetch_array( $result )) {
	
		$ArraySeatsTaken[] = $row['seat_number'];

	} 

	$ArraySeatsAvail = array_diff($ArraySeats, $ArraySeatsTaken);

	// Set sequential keys for the array.

	$ArraySeatsAvail = array_values($ArraySeatsAvail);

	$seat_number[1] = $ArraySeatsAvail[0];
	$seat_number[2] = $ArraySeatsAvail[1];
	$seat_number[3] = $ArraySeatsAvail[2];
	$seat_number[4] = $ArraySeatsAvail[3];
	$seat_number[5] = $ArraySeatsAvail[4];

Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Allocated Seating Help

Post by Eric! »

I'll just throw this out there as an idea. Since the number of seats is the same, have you thought about separating your data into Rows and Numbers? This way you can easily search for rows meeting your criteria (3 adjacent empty, etc.) And your placement in that row can be done numerically and the same no matter which row you're trying to fill.

For example here's an empty theater array:

Code: Select all

$letters = range('A', 'E');
foreach($letters as $letter){
 for($i=1;$i<26;$i++){
   $seats[$letter][$i]=false; //false is not sold
 }
}
Then you can start filling $seats['E'] and match the seats to your center # +/- # seats requested. Or jump to the next row if you can't find enough available seats. And if you run out of rows you then have to just start placing individual or smaller groups scattered or staggered by rows. This way 4 friends could get E1, E2 and then right behind them you could place D1, D2. I think it might make searching for seating patterns easier.

You could still store them in the database as "E12" and just parse the row/seat for your code.

BTW, what happened to picking your own seats?
superlative
Forum Newbie
Posts: 10
Joined: Wed Dec 04, 2013 10:24 pm

Re: Allocated Seating Help

Post by superlative »

Hi Eric,

Seems like an easier way. I want to still want to read it from the database and store in the database as C12 and C15 for example.
I'm too stupid to know based on your code how to integrate it.

I guess your code it makes an array $seats[] with a value being for example A --> 12 and A -- > 13 and it looks like the third part of the array is a true or false?

How do I integrate in my existing code? And then based on that I assume for the array I take 5 values, and then check the [row] part of the array is the same for up to 5 variables for the seats?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Allocated Seating Help

Post by Eric! »

Well, it might not be the best approach for you if you aren't familiar with multi-dimensional arrays. I just looked at it and thought about how I might try to solve it. I have to run right now, but maybe someone can help you build upon what you already have.
superlative
Forum Newbie
Posts: 10
Joined: Wed Dec 04, 2013 10:24 pm

Re: Allocated Seating Help

Post by superlative »

Thanks Eric,
I will wait and see if someone else can help.
Yeah the multi-dimensional arrays I have played with a bit, but it does get extremely confusing.
Based on what you have said I need to split my array to have the Row as a seperate level on the array, and then I need to build on the checks to make sure that the 5 variables all have the same Row number, if not search for another set.
And then I need to jumble it up so that it starts from the middle of the row not from left to right.

Most people want to be int he centre of a row, would be not a good scenario to have a half full cinema and have most people on the left hand side.

If no one helps, if you do come back i'd owe you a million :(
superlative
Forum Newbie
Posts: 10
Joined: Wed Dec 04, 2013 10:24 pm

Re: Allocated Seating Help

Post by superlative »

Hi Eric,

I have tested your code and it is much neater to generate the array being separated in to having the row separate from the seat number.
I did the var_dump and it outputs the following:

Code: Select all

array(5) {
  ["A"]=>
  array(25) {
    [1]=>
    bool(false)
    [2]=>
    bool(false)
    [3]=>
    bool(false)
    [4]=>
    bool(false)
    [5]=>
    bool(false)
    [6]=>
    bool(false)
    [7]=>
    bool(false)
    [8]=>
    bool(false)
    [9]=>
    bool(false)
    [10]=>
    bool(false)
    [11]=>
    bool(false)
    [12]=>
    bool(false)
    [13]=>
    bool(false)
    [14]=>
    bool(false)
    [15]=>
    bool(false)
    [16]=>
    bool(false)
    [17]=>
    bool(false)
    [18]=>
    bool(false)
    [19]=>
    bool(false)
    [20]=>
    bool(false)
    [21]=>
    bool(false)
    [22]=>
    bool(false)
    [23]=>
    bool(false)
    [24]=>
    bool(false)
    [25]=>
    bool(false)
  }
  ["B"]=>
  array(25) {
    [1]=>
    bool(false)
    [2]=>
    bool(false)
    [3]=>
    bool(false)
    [4]=>
    bool(false)
    [5]=>
    bool(false)
    [6]=>
    bool(false)
    [7]=>
    bool(false)
    [8]=>
    bool(false)
    [9]=>
    bool(false)
    [10]=>
    bool(false)
    [11]=>
    bool(false)
    [12]=>
    bool(false)
    [13]=>
    bool(false)
    [14]=>
    bool(false)
    [15]=>
    bool(false)
    [16]=>
    bool(false)
    [17]=>
    bool(false)
    [18]=>
    bool(false)
    [19]=>
    bool(false)
    [20]=>
    bool(false)
    [21]=>
    bool(false)
    [22]=>
    bool(false)
    [23]=>
    bool(false)
    [24]=>
    bool(false)
    [25]=>
    bool(false)
  }
  ["C"]=>
  array(25) {
    [1]=>
    bool(false)
    [2]=>
    bool(false)
    [3]=>
    bool(false)
    [4]=>
    bool(false)
    [5]=>
    bool(false)
    [6]=>
    bool(false)
    [7]=>
    bool(false)
    [8]=>
    bool(false)
    [9]=>
    bool(false)
    [10]=>
    bool(false)
    [11]=>
    bool(false)
    [12]=>
    bool(false)
    [13]=>
    bool(false)
    [14]=>
    bool(false)
    [15]=>
    bool(false)
    [16]=>
    bool(false)
    [17]=>
    bool(false)
    [18]=>
    bool(false)
    [19]=>
    bool(false)
    [20]=>
    bool(false)
    [21]=>
    bool(false)
    [22]=>
    bool(false)
    [23]=>
    bool(false)
    [24]=>
    bool(false)
    [25]=>
    bool(false)
  }
  ["D"]=>
  array(25) {
    [1]=>
    bool(false)
    [2]=>
    bool(false)
    [3]=>
    bool(false)
    [4]=>
    bool(false)
    [5]=>
    bool(false)
    [6]=>
    bool(false)
    [7]=>
    bool(false)
    [8]=>
    bool(false)
    [9]=>
    bool(false)
    [10]=>
    bool(false)
    [11]=>
    bool(false)
    [12]=>
    bool(false)
    [13]=>
    bool(false)
    [14]=>
    bool(false)
    [15]=>
    bool(false)
    [16]=>
    bool(false)
    [17]=>
    bool(false)
    [18]=>
    bool(false)
    [19]=>
    bool(false)
    [20]=>
    bool(false)
    [21]=>
    bool(false)
    [22]=>
    bool(false)
    [23]=>
    bool(false)
    [24]=>
    bool(false)
    [25]=>
    bool(false)
  }
  ["E"]=>
  array(25) {
    [1]=>
    bool(false)
    [2]=>
    bool(false)
    [3]=>
    bool(false)
    [4]=>
    bool(false)
    [5]=>
    bool(false)
    [6]=>
    bool(false)
    [7]=>
    bool(false)
    [8]=>
    bool(false)
    [9]=>
    bool(false)
    [10]=>
    bool(false)
    [11]=>
    bool(false)
    [12]=>
    bool(false)
    [13]=>
    bool(false)
    [14]=>
    bool(false)
    [15]=>
    bool(false)
    [16]=>
    bool(false)
    [17]=>
    bool(false)
    [18]=>
    bool(false)
    [19]=>
    bool(false)
    [20]=>
    bool(false)
    [21]=>
    bool(false)
    [22]=>
    bool(false)
    [23]=>
    bool(false)
    [24]=>
    bool(false)
    [25]=>
    bool(false)
  }
}
So I just need help in to reading my MySQL results into the same format in a seperate array, and they cross checking to change the flags false to true for seats taken, and then being able to do a search from the most middle row for 5 seats.
superlative
Forum Newbie
Posts: 10
Joined: Wed Dec 04, 2013 10:24 pm

Re: Allocated Seating Help

Post by superlative »

While i'm waiting to see if anyone else can help.

I have figured out that I can use the following code to get the row and seat number from my database read in my existing code:

Code: Select all

$db_seat = "A15";
$current_row = $db_seat[0];
$current_seat = substr($db_seat, 1);

echo $current_row;
echo "<BR>";
echo $current_seat;
So to get the row number i use [0] to just get the first letter for the row number.
To get the seat number I use sbustr with an offset of 1 to skip on the letter of the row and just get the seat number.

Any help in integrating the code Eric provided, with the above to cross reference would help. Then I just need to make sure that the seat numbers are the middle most and all are in the same row.

Getting there :(
superlative
Forum Newbie
Posts: 10
Joined: Wed Dec 04, 2013 10:24 pm

Re: Allocated Seating Help

Post by superlative »

Where for art thou Eric :(
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Allocated Seating Help

Post by Eric! »

Looks like you have it mostly figured out.

Loop through your database data to set a seat to be occupied:

Code: Select all

$seat[$current_row][$current_seat]=true;
To loop through and find empty seats you just test it to see if it is true or false. So when picking seats, you know the $seat_count (1 to 5 tickets) and then you just search through your array for open spots. This is a little trickier because you want to fill from the center outwards.

Normally I don't provide the full code, but I like these types of puzzles and I'm waiting on a long FTP upload to finish. I tested this a little bit, but you'll need to put it though it's paces and probably modify it. Take a look at it well because if you don't understand it don't use it and I didn't test it much! Admittedly it's a bit sloppy, but you get the idea.

This code gives up if a block of people don't fit together. You'll need to make another routine to handle searching for sub-groups of people and placing them individually.

Code: Select all

<?php
//test
$seat_size='4';
//test empty theater
$letters = range('A', 'E');
foreach($letters as $letter){
 for($i=1;$i<26;$i++){
   $seats[$letter][$i]=false; //false is not sold
 }
}

for($i=2;$i<23;$i++)
{
  // 1 on left and 2 on right
  $seats['E'][$i]=true; //book some seats for testing
  echo "Filling E$i\n";
}

//Search for spots
$foundEmpty=array();
$letters = range('E', 'A');
foreach($letters as $letter){
 for($i=12+floor($seat_size/2);$i>0;$i--){
   if($seats[$letter][$i]==false){
       $anyOccupied=false;
       for($j=0;$j<$seat_size;$j++){ //scan ahead and see if any are booked
          if(($i-$j)>0) {
            if($seats[$letter][$i-$j]) $anyOccupied=true; //someone is in the block of seats
          }
          else{//group won't fit on end
            $anyOccupied=true;
            break;
          }
       }
       if(!$anyOccupied){ // book the block
          for($j=0;$j<$seat_size;$j++){
             $seats[$letter][($i-$j)]=true; // record them in array
             $foundEmpty[]=$letter.($i-$j); // make a handy list
          }//fill them
          break;
       }
   }
 }
 if(count($foundEmpty)<$seat_size){
   for($i=12+floor($seat_size/2);$i<26;$i++){
     if($seats[$letter][$i]==false){
         $anyOccupied=false;
         for($j=0;$j<$seat_size;$j++){ //scan ahead and see if any are booked
            if(($i+$j)<26) {
              if($seats[$letter][$i+$j]) $anyOccupied=true; //someone is in the block of seats
            }
            else
            {
	       //group won't fit on end
               $anyOccupied=true;
               break;
            }
         }
         if(!$anyOccupied){ // book the block
            for($j=0;$j<$seat_size;$j++){
               $seats[$letter][($i+$j)]=true; // record them in array
               $foundEmpty[]=$letter.($i+$j); // make a handy list
            }//fill them
            break;
         }
     }
   }
 }
 if(count($foundEmpty)==$seat_size) break;
}
var_dump($foundEmpty);
?>
superlative
Forum Newbie
Posts: 10
Joined: Wed Dec 04, 2013 10:24 pm

Re: Allocated Seating Help

Post by superlative »

Hi Eric,

Thanks for your help so far. I appreciate it immensley.
And yes it is an extremely tricky thing.

I have added to your code an asort to the array so that the returned values are in seat order number.
It appears to work well.

Here is my updated code of yours as I have tried to understand it.

Code: Select all

<?php

// START VARIABLES

// Number of seats we need to find in a row.
$seat_size='4';

// END VARIABLES



//Create available seat numbers that exist in cinema
//Rows A to E with seat numbers 1 to 25.
$letters = range('A', 'E');
foreach($letters as $letter){
 for($i=1;$i<26;$i++){
   $seats[$letter][$i]=false; //false is not sold
 }
}



// Make test example seats booked E2 to E22.
for($i=2;$i<23;$i++)
{
  // 1 on left and 2 on right
  $seats['E'][$i]=true; //book some seats for testing
  echo "Filling E$i\n<BR>";
}

// Add also seats booked as example D9 to D25
for($i=9;$i<26;$i++)
{
  // 1 on left and 2 on right
  $seats['D'][$i]=true; //book some seats for testing
  echo "Filling D$i\n<BR>";
}




//Search for spots
$foundEmpty=array();
$letters = range('E', 'A');
foreach($letters as $letter){
 for($i=12+floor($seat_size/2);$i>0;$i--){
   if($seats[$letter][$i]==false){
       $anyOccupied=false;
       for($j=0;$j<$seat_size;$j++){ //scan ahead and see if any are booked
          if(($i-$j)>0) {
            if($seats[$letter][$i-$j]) $anyOccupied=true; //someone is in the block of seats
          }
          else{//group won't fit on end
            $anyOccupied=true;
            break;
          }
       }
       if(!$anyOccupied){ // book the block
          for($j=0;$j<$seat_size;$j++){
             $seats[$letter][($i-$j)]=true; // record them in array
             $foundEmpty[]=$letter.($i-$j); // make a handy list
          }//fill them
          break;
       }
   }
 }
 if(count($foundEmpty)<$seat_size){
   for($i=12+floor($seat_size/2);$i<26;$i++){
     if($seats[$letter][$i]==false){
         $anyOccupied=false;
         for($j=0;$j<$seat_size;$j++){ //scan ahead and see if any are booked
            if(($i+$j)<26) {
              if($seats[$letter][$i+$j]) $anyOccupied=true; //someone is in the block of seats
            }
            else
            {
               //group won't fit on end
               $anyOccupied=true;
               break;
            }
         }
         if(!$anyOccupied){ // book the block
            for($j=0;$j<$seat_size;$j++){
               $seats[$letter][($i+$j)]=true; // record them in array
               $foundEmpty[]=$letter.($i+$j); // make a handy list
            }//fill them
            break;
         }
     }
   }
 }
 if(count($foundEmpty)==$seat_size) break;
}


// Sort the array of seats found alphabetically
asort($foundEmpty);

echo "<BR>";
var_dump($foundEmpty);


?>
With the code I make seats E2 to E22 booked, and D9 to D25 booked.
It outputs the following:

Filling E2
Filling E3
Filling E4
Filling E5
Filling E6
Filling E7
Filling E8
Filling E9
Filling E10
Filling E11
Filling E12
Filling E13
Filling E14
Filling E15
Filling E16
Filling E17
Filling E18
Filling E19
Filling E20
Filling E21
Filling E22
Filling D9
Filling D10
Filling D11
Filling D12
Filling D13
Filling D14
Filling D15
Filling D16
Filling D17
Filling D18
Filling D19
Filling D20
Filling D21
Filling D22
Filling D23
Filling D24
Filling D25

array(4) { [3]=> string(2) "D5" [2]=> string(2) "D6" [1]=> string(2) "D7" [0]=> string(2) "D8" }
Next question. When people book in a theatre, sometimes they dont want to be in the very front. Can I easily change the code to be D, C, B, A, E?
superlative
Forum Newbie
Posts: 10
Joined: Wed Dec 04, 2013 10:24 pm

Re: Allocated Seating Help

Post by superlative »

And I think the final question that I didnt think of but others have mentioned to me that the ends of each row are not the best to be in.
So, I don't know how easy it would be to modify this beautiful code you have.

But would it be possible when it is doing the search, to for instance fill up 80% of each row before selling the final seats in that row during the search.
For example seats 1-5 and 20-25, if we could skip those in each row in the search unless there are no other seats to choose from in other rows.

Eric, I will have to send you a paypal payment. You have been an absolute star here. I'm mainly vb.net background and only intermediate php. :(
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Allocated Seating Help

Post by Eric! »

I would suggest you test a variety of test case scenarios with the code -- to make sure there's no bugs. Then save your various test conditions so you can retest with whatever changes you make later to the algorithm.

You can vary the start and stop points however you want. The code just does a binary search starting in the middle working left to the boundary, then again from the middle working right to the boundary. You can change either the rows or the seating bounds.

Left bounds:

Code: Select all

for($i=12+floor($seat_size/2);$i>0;$i--){ // change $i>5
Right bounds

Code: Select all

for($i=12+floor($seat_size/2);$i<26;$i++){ // change $i<20
Skipping the first row

Code: Select all

$letters = range('D', 'A'); // skip E and do D to A
Also you could assign weights to desired seating spots, search the whole theater and then place them in the highest weighted positions available (of course this is subjective). But there's no way to program in a way to determine for the buyer not to be in the front row. They would have to request a row or area, or better yet, pick empty seats manually.

EDIT: I should mention that this code is Case Sensitive, so row A is different from row "a" just in case your database has data in it from another source that is using a different case.

Also if it fails to find seats with the narrower boundaries, run it again with full boundaries and if it fails to fit then, you'll have to place them in smaller sub-groups or individually.
superlative
Forum Newbie
Posts: 10
Joined: Wed Dec 04, 2013 10:24 pm

Re: Allocated Seating Help

Post by superlative »

Thanks Eric.

For "$letters = range('D', 'A'); // skip E and do D to A", this would leave E out all together from the formula? So maybe I need to do D to A, and then tack on E on the end? As E still needs to be sold just sold last?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Allocated Seating Help

Post by Eric! »

By the way, if you're running this in the command line I made a little display.

Code: Select all

echo "Theater\n\n";
foreach($letters as $letter){
 echo "ROW $letter: ";
 for($i=1;$i<26;$i++){
   echo ($seats[$letter][$i]) ? "X " : ". ";
 }
 echo "\n";
}
[text]Theater

ROW E: . X X X X X X X X X X X X X X X X X X X X X . . .
ROW D: . . . . . . . . . . X X X X . . . . . . . . . . .
ROW C: . . . . . . . . . . . . . . . . . . . . . . . . .
ROW B: . . . . . . . . . . . . . . . . . . . . . . . . .
ROW A: . . . . . . . . . . . . . . . . . . . . . . . . .
[/text]
You can use this to discuss seating situations and visually check how the audience would look.
superlative
Forum Newbie
Posts: 10
Joined: Wed Dec 04, 2013 10:24 pm

Re: Allocated Seating Help

Post by superlative »

Hi Eric,

Your chart looks great!

I have tried to alter my code so that seats 1-5 and 21-25 representing 5 seats left and 5 seats right are not sold however it still seems to sell them.
The chart helps to problem solve the code.

Here is my current code. If you could copy paste it would be appreciated and try.

Code: Select all

<?php

// START VARIABLES

// Number of seats we need to find in a row.
$seat_size='6';

// END VARIABLES



//Create available seat numbers that exist in cinema
//Rows A to E with seat numbers 1 to 25.
$letters = range('A', 'E');
foreach($letters as $letter){
 for($i=1;$i<26;$i++){
   $seats[$letter][$i]=false; //false is not sold
 }
}



// Make test example seats booked E2 to E22.
for($i=2;$i<23;$i++)
{
  // 1 on left and 2 on right
  $seats['E'][$i]=true; //book some seats for testing
  echo "Filling E$i\n<BR>";
}

// Add also seats booked as example D9 to D25
for($i=9;$i<26;$i++)
{
  // 1 on left and 2 on right
  $seats['D'][$i]=true; //book some seats for testing
  echo "Filling D$i\n<BR>";
}




//Search for spots
$foundEmpty=array();
$letters = range('D', 'A');  // skip E and do D to A
foreach($letters as $letter){
 for($i=12+floor($seat_size/2);$i>5;$i--){ // $i>5 so that seats 1-5 are not sold   
   if($seats[$letter][$i]==false){
       $anyOccupied=false;
       for($j=0;$j<$seat_size;$j++){ //scan ahead and see if any are booked
          if(($i-$j)>0) {
            if($seats[$letter][$i-$j]) $anyOccupied=true; //someone is in the block of seats
          }
          else{//group won't fit on end
            $anyOccupied=true;
            break;
          }
       }
       if(!$anyOccupied){ // book the block
          for($j=0;$j<$seat_size;$j++){
             $seats[$letter][($i-$j)]=true; // record them in array
             $foundEmpty[]=$letter.($i-$j); // make a handy list
          }//fill them
          break;
       }
   }
 }
 if(count($foundEmpty)<$seat_size){
   for($i=12+floor($seat_size/2);$i<21;$i++){ // $i<21 so that seats 21-25 are not sold. 
     if($seats[$letter][$i]==false){
         $anyOccupied=false;
         for($j=0;$j<$seat_size;$j++){ //scan ahead and see if any are booked
            if(($i+$j)<26) {
              if($seats[$letter][$i+$j]) $anyOccupied=true; //someone is in the block of seats
            }
            else
            {
               //group won't fit on end
               $anyOccupied=true;
               break;
            }
         }
         if(!$anyOccupied){ // book the block
            for($j=0;$j<$seat_size;$j++){
               $seats[$letter][($i+$j)]=true; // record them in array
               $foundEmpty[]=$letter.($i+$j); // make a handy list
            }//fill them
            break;
         }
     }
   }
 }
 if(count($foundEmpty)==$seat_size) break;
}


// Sort the array of seats found alphabetically
asort($foundEmpty);

echo "<BR>";
var_dump($foundEmpty);


// Below is Eric's theatre layout check.
echo "<BR>";
echo "<BR>Theater<BR>";
foreach($letters as $letter){
 echo "ROW $letter: ";
 for($i=1;$i<26;$i++){
   echo ($seats[$letter][$i]) ? " 1 " : " 0 ";
 }
 echo "<BR>";
}



?>
Ultimately once I get your code working, I think I will just integrate it as a function like FunctionEmptySeats(5) and it will run the above code and return me 5 seats in a row that are available.
Post Reply