Jump to content

Coloring Individual Cells According To Array


Poopfeast

Recommended Posts

Hi, as I'm hosting a local LAN I need to create a map of all the reserved and possible seats according to database information.

I currently have all reserved seat information stored in an array like this:

Array ( [0] => A2 [1] => D7 [2] => C10 [3] => [4] => [5] => [6] => [7] => F9 [8] => [9] => [10] => F1 [11] => F8 [12] => [13] => C8 [14] => [15] => [16] => [17] => F10 [18] => F4 [19] => F2 [20] => [21] => [22] => [23] => F5 [24] => F3 [25] => [26] => [27] => F7 [28] => [29] => D6 [30] => [31] => [32] => [33] => [34] => [35] => [36] => [37] => [38] => D4

Here's the table code

for ($i = 1; $i <= 10; $i++) {
if (array_key_exists('A'.$i, $array))
{
$color = 'red';
}
else ($color = 'green');
echo "<table border='1' width=200 height=40>";
echo "<tr>";
echo "<td bgcolor=".$color.">A" . $i . "</td>";
echo "<td bgcolor=".$color.">B" . $i . "</td>";
echo "<td bgcolor=".$color.">C" . $i . "</td>";
echo "<td bgcolor=".$color.">D" . $i . "</td>";
echo "<td bgcolor=".$color.">E" . $i . "</td>";
echo "<td bgcolor=".$color.">F" . $i . "</td>";
echo "</tr>";
echo "</table>";
}

 

Which prints a table with A1 to A10 and B1 to B10 etc.

Now the problem is that I don't know how to check each seat if they're already taken(in the array) and make it so that it returns red(seat taken) for each individual cell.

I was hoping someone could help me with this, it'd be a huge help!

 

Thanks for reading this.

I think you need to reconsider how you are going about this.

 

The way your loop structure is now, it's going to set all seats in a 'row' to whatever color the 'A' seat is. For instance all seats 3 are going to be the same color as 'A3'.

 

Also, I'm not convinced 'array_key_exists' is what you want to be using.

 

I'd recommend attempting to use nested for loops. The inner loop being cells within a row, and the outer loop being the rows themselves.

I made some assumptions about the seats and such, but this seems to do what you need it to.

 

$array = array ( 0 => 'A2', 1 => 'D7', 2 => 'C10', 3 =>'', 4 =>'', 5 =>'', 6 =>'', 7 => 'F9', 8 =>'', 9 =>'', 10 => 'F1', 11 => 'F8', 12 =>'', 13 => 'C8', 14 =>'', 15 =>'', 16 =>'', 17 => 'F10', 18 => 'F4', 19 => 'F2', 20 => '', 21 =>'', 22 =>'', 23 => 'F5', 24 => 'F3', 25 =>'', 26 =>'', 27 => 'F7', 28 =>'', 29 => 'D6', 30 =>'', 31 => '', 32 =>'', 33 =>'', 34 =>'', 35 =>'', 36 =>'', 37 =>'', 38 => 'D4');

$rows = range( 1, 10 );
$seats = range( 'A', 'F' );

echo "<table border='1' width='200' height='40'>\n";
foreach( $rows as $row_v ) {
   echo "<tr>\n";
   foreach( $seats as $seat_v ) {
       $color = in_array($seat_v.$row_v, $array) ? 'red' : 'green';
       echo "<td bgcolor=".$color.">" . $seat_v . $row_v . "</td>\n";
   }
   echo "</tr>\n";
}
echo "</table>\n";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.