I'm producing a room timetable page for where I work. I have virtually finished it and use the following to display the records:
echo"<TABLE border=1 width=100%>";
for ($i=0; $i<$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo"<TR><TD>$row[day]</TD>
<TD>$row[date]</TD>
<TD>$row[period1]</TD>
<TD>$row[period2]</TD>
<TD>$row[period3]</TD>
<TD>$row[period4]</TD>
<TD>$row[period5]</a></TD>
<TD>$row[period6]</TD>
<TD><a href='http://www.htmlforums.com/archive/index.php/edit.php?id=$row[id]&room=$room'>Edit</a></TD></TR>";
}
echo"</TABLE>";
This works OK. What I want to do is, if any of the records between period1 - period6 = "Free" I want them to be displayed in a different colour. I initially thought I'd be able to do it with if statements but I can't figure out how or where to put them.
Any help would be appreciated.
Cheers.yes, you have to use if statements. but to make it faster and less code I would use a loop as well. and you can color the owrds or the cell, your choice, also make sure you use quotes in you variables. the only time you don't need them is if you use $row[1] where teh 1 is the second column of your table.
echo"<table border=\"1\" width=\"100%\">";
while($row = mysql_fetch_array($result)){
echo"<TR><TD>{$row["day"]}</TD>\n
<TD>{$row["date"]}</TD>\n";
for ($x=1; $x<6; $x++){
($row["period{$x}"] == "free")?$color = "bgcolor = \"#ff0000\"":$color ="";
echo"<TD $color>{$row["period{$x}"]}</TD>\n";
}
echo"<TD><a href=http://www.htmlforums.com/archive/index.php/\"edit.php?id={$row["id"]}&room=$room\">Edit</a></TD></TR>";
}
echo"</table>";
that should work. there is nothing wrong with using the for loop you had but if you don't use the $i variable then there is no reason to use it. so instead use the while loop
echo"<TABLE border=1 width=100%>";
for ($i=0; $i<$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo"<TR><TD>$row[day]</TD>
<TD>$row[date]</TD>
<TD>$row[period1]</TD>
<TD>$row[period2]</TD>
<TD>$row[period3]</TD>
<TD>$row[period4]</TD>
<TD>$row[period5]</a></TD>
<TD>$row[period6]</TD>
<TD><a href='http://www.htmlforums.com/archive/index.php/edit.php?id=$row[id]&room=$room'>Edit</a></TD></TR>";
}
echo"</TABLE>";
This works OK. What I want to do is, if any of the records between period1 - period6 = "Free" I want them to be displayed in a different colour. I initially thought I'd be able to do it with if statements but I can't figure out how or where to put them.
Any help would be appreciated.
Cheers.yes, you have to use if statements. but to make it faster and less code I would use a loop as well. and you can color the owrds or the cell, your choice, also make sure you use quotes in you variables. the only time you don't need them is if you use $row[1] where teh 1 is the second column of your table.
echo"<table border=\"1\" width=\"100%\">";
while($row = mysql_fetch_array($result)){
echo"<TR><TD>{$row["day"]}</TD>\n
<TD>{$row["date"]}</TD>\n";
for ($x=1; $x<6; $x++){
($row["period{$x}"] == "free")?$color = "bgcolor = \"#ff0000\"":$color ="";
echo"<TD $color>{$row["period{$x}"]}</TD>\n";
}
echo"<TD><a href=http://www.htmlforums.com/archive/index.php/\"edit.php?id={$row["id"]}&room=$room\">Edit</a></TD></TR>";
}
echo"</table>";
that should work. there is nothing wrong with using the for loop you had but if you don't use the $i variable then there is no reason to use it. so instead use the while loop