Reputation: 55
I have this piece of code which changes the colour of a HTML cell based on the Value. Its function is to display if the slots (out of 3) are full/empty/have space.
I have only included one of the IF statements for 9AM. I have duplicated and altered the code for each time frame, so 10Am, 11AM, 12PM etc.. Which takes up a lot of lines of code and doesn't look very good.
Is there are a way to simplify this for all columns in my table?
Upvotes: 1
Views: 55
Reputation:
If you just want to simplify the code, try to use this:
$result = mysql_query($query); if(!$result) { die("MySQL query failed: " . mysql_error()); } else{ print ''; print ""; while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { if(strpos($row['9AM'], '(3)')) { $nineam="#00FF00"; } else if (empty($row['9AM'])){ $nineam = ""; } else if (strpos($row['9AM'], '(1)')){ $nineam ="#00FF00"; } else if (strpos($row['9AM'], '(2)')){ $nineam ="#00FF00"; } else if (strpos($row['9AM'], '(0)')){ $nineam ="#FF0000"; } print " ";
<table>
<tr>
<td><strong>May</strong></td>
<td><strong>9AM</strong></td>
<td><strong>10AM</strong></td>
<td><strong>11AM</strong></td>
<td><strong>12PM</strong></td>
<td><strong>13PM</strong></td>
<td><strong>14PM</strong></td>
<td><strong>15PM</strong></td>
<td><strong>16PM</strong></td>
</tr>
<tr>
<td>{$row['Day']}</td>
<td bgcolor="\"$nineam\"">{$row['9AM']}</td>
<td bgcolor="\"$tenam\"">{$row['10AM']}</td>
<td bgcolor="\"$elevenam\"">{$row['11AM']}</td>
<td bgcolor="\"$twelvpm\"">{$row['12PM']}</td>
<td bgcolor="\"$onepm\"">{$row['13PM']}</td>
<td bgcolor="\"$twopm\"">{$row['14PM']}</td>
<td>{$row['15PM']}</td>
<td>{$row['16PM']}</td>
</tr>
Upvotes: 1