I'm creating a little booking system. Rows are set to current week columns to a specific time.
What I'm trying to do is to change day names to a specific colour. Blue in the past, date current in green. Somehow the if
statement isn't doing anything. Could anyone explain or point me in the right direction?
$today = time();
$lastWeek = [];
$lastWeek[0] = date('D',strtotime('last monday'));
$lastWeek[1] = date('D',strtotime('last monday +1day'));
$lastWeek[2] = date('D',strtotime('last monday +2day'));
$lastWeek[3] = date('D',strtotime('last monday +3day'));
$lastWeek[4] = date('D',strtotime('last monday +4day'));
$timeSlot = [];
$timeSlot [0] = date('H:i', strtotime('9:00')) ."-9:30";
$timeSlot [1] = date('H:i', strtotime('9:30')) ."-10:00";
$timeSlot [2] = date('H:i', strtotime('10:00')) ."-10:30";
$timeSlot [3] = date('H:i', strtotime('10:30')) ."-11:00";
$timeSlot [4] = date('H:i', strtotime('11:00')) ."-11:30";
$timeSlot [5] = date('H:i', strtotime('11:30')) ."-12:00";
echo "<html><head><title>Doctor Booking timetable</title></head>";
echo "<body><table id=myTable border=2 align=center >";
echo "<tr><td>Week Commencing</td>";
foreach ($timeSlot as $time)
{
echo"<td>";
echo $time;
echo"</td>";
}
echo "</tr>";
foreach ($lastWeek as $day)
{
echo "<tr><td>";
echo $day;
echo "</td></tr>";
}
if ($lastWeek < $today){
echo "<font color='blue'></font>";
}else {
echo "<font color='green'></font>";
}
You need to put the if
statement inside the foreach
(like your post title suggests) and also wrap $day
inside your colour tags. FYI the font
tag has been deprecated for a long time and you should use span
instead.
In your question, you are comparing $lastWeek
with $today
- $lastWeek
is an array, which you are looping through. As per your foreach
, the current iteration will be contained in the $day
variable, so you should compare that to $today
:
foreach ($lastWeek as $day)
{
echo "<tr><td>";
$color = ($day < $today) ? 'blue' : 'green';
echo '<span style="color: ' . $color . '">' . $day . '</span>';
echo "</td></tr>";
}
PS: I've used the ternary operator here for your if statement. It's the same thing as writing:
if($day < $today)
$color = 'blue';
else
$color = 'green';
... but shorter
PPS. I'm assuming here that your dates are in the correct format to compare using a GT/LT operator. If they aren't, you'll need to format them using something like the date function .
I would not use the deprecated tag unless it was very much needed. See about this in http://www.w3schools.com/tags/tag_font.asp
Using your code style:
$today = time();
$lastWeek = array();
$lastWeek[0] = date('D',strtotime('last monday'));
$lastWeek[1] = date('D',strtotime('last monday +1day'));
$lastWeek[2] = date('D',strtotime('last monday +2day'));
$lastWeek[3] = date('D',strtotime('last monday +3day'));
$lastWeek[4] = date('D',strtotime('last monday +4day'));
$timeSlot = array();
$timeSlot [0] = date('H:i', strtotime('9:00')) ."-9:30";
$timeSlot [1] = date('H:i', strtotime('9:30')) ."-10:00";
$timeSlot [2] = date('H:i', strtotime('10:00')) ."-10:30";
$timeSlot [3] = date('H:i', strtotime('10:30')) ."-11:00";
$timeSlot [4] = date('H:i', strtotime('11:00')) ."-11:30";
$timeSlot [5] = date('H:i', strtotime('11:30')) ."-12:00";
echo "<html><head><title>Doctor Booking timetable</title></head>";
echo "<body><table id=myTable border=2 align=center >";
echo "<tr><td>Week Commencing</td>";
foreach ($timeSlot as $time)
{
echo"<td>";
echo $time;
echo"</td>";
}
echo "</tr>";
foreach ($lastWeek as $day)
{
echo "<tr><td>";
if ($lastWeek < $today){
echo "<font color='blue'>$day</font>";
}else {
echo "<font color='green'>$day</font>";
}
echo "</td></tr>";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.