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.

Edited by Zane
Link to comment
Share on other sites

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.

Edited by deoiub
Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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.