2014-01-19 316 views
0

我正在創建一個小預訂系統。行被設置爲當前周的列到特定時間。如果語句在foreach循環中

我想要做的是將日期名稱更改爲特定的顏色。藍色在過去,日期目前在綠色。不知何故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>"; 
} 
+6

的'if'語句是*不*了'foreach'循環中。 – Boaz

+0

你也需要查看一些HTML教程,你的html無效。 '$ lastWeek'是一個數組,它不會評估爲真 –

+0

foreach循環中if語句的語法是什麼?出現了很多錯誤。謝謝。 – user3204192

回答

2

你需要把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>"; 
} 

PS:我用的ternary operator在這裏爲您的if語句。這是同樣的事情寫:

if($day < $today) 
    $color = 'blue'; 
else 
    $color = 'green'; 

...但短

PPS。我假設您的日期格式正確,可以使用GT/LT運算符進行比較。如果不是,則需要使用類似date function的格式對其進行格式化。

+0

OP還將當前時間戳「$ today」與數組「$ lastWeek」進行比較,而不是使用數組元素「$ day」。你似乎已經糾正了這個疏忽,但也可能特別解釋了這一點。 – Boaz

+1

@Boaz加爲參考 - 歡呼 –

+0

感謝您的幫助,這是排序我的問題,我也牢記你說的日期。 – user3204192

0

我不會使用已棄用的標籤,除非它非常需要。 查看關於這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>"; 
}