Poopfeast Posted November 19, 2012 Share Posted November 19, 2012 (edited) 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 November 19, 2012 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/270910-coloring-individual-cells-according-to-array/ Share on other sites More sharing options...
Beeeeney Posted November 19, 2012 Share Posted November 19, 2012 I can't answer your question but kudos for the username. Quote Link to comment https://forums.phpfreaks.com/topic/270910-coloring-individual-cells-according-to-array/#findComment-1393597 Share on other sites More sharing options...
deoiub Posted November 19, 2012 Share Posted November 19, 2012 (edited) 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 November 19, 2012 by deoiub Quote Link to comment https://forums.phpfreaks.com/topic/270910-coloring-individual-cells-according-to-array/#findComment-1393604 Share on other sites More sharing options...
Pikachu2000 Posted November 19, 2012 Share Posted November 19, 2012 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"; Quote Link to comment https://forums.phpfreaks.com/topic/270910-coloring-individual-cells-according-to-array/#findComment-1393615 Share on other sites More sharing options...
Poopfeast Posted November 19, 2012 Author Share Posted November 19, 2012 Thanks alot guys, I've got it working now Quote Link to comment https://forums.phpfreaks.com/topic/270910-coloring-individual-cells-according-to-array/#findComment-1393633 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.