我正在创建一个小小的预订系统。将行设置为当前周,将列设置为特定时间。
我想要做的是将日期名称更改为特定的颜色。过去是蓝色,现在是绿色。不知何故,if语句什么也没做。谁能给我解释一下或者给我指出正确的方向?
$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>";
}发布于 2014-01-20 05:31:41
您需要将if语句放在foreach中(正如您的帖子标题所建议的那样),并将$day包装在您的颜色标记中。仅供参考,font标记已经被弃用了很长一段时间,您应该使用span。
在您的问题中,您正在将$lastWeek与$today进行比较- $lastWeek是一个您正在遍历的数组。根据您的foreach,当前迭代将包含在$day变量中,因此您应该将与$today进行比较
foreach ($lastWeek as $day)
{
echo "<tr><td>";
$color = ($day < $today) ? 'blue' : 'green';
echo '<span style="color: ' . $color . '">' . $day . '</span>';
echo "</td></tr>";
}附言:我在这里为你的if语句使用了ternary operator。这和写东西是一样的:
if($day < $today)
$color = 'blue';
else
$color = 'green';..。但更短
PPS。我在这里假设您的日期格式正确,可以使用GT/LT运算符进行比较。如果不是,则需要使用类似于date function的格式。
发布于 2014-01-20 05:58:20
除非非常需要,否则我不会使用已弃用的标记。请在http://www.w3schools.com/tags/tag_font.asp中查看此内容
使用你的代码风格:
$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>";
}https://stackoverflow.com/questions/21222898
复制相似问题