2012-05-03 107 views
0

我有一個表,日期,星期&天,例如:2012-05-01 ,週二 2012-05-02,週三等顯示在PHP一個簡單的日曆

我試圖在像桌子這樣的日曆中顯示它。問題在於,它每天都會將其顯示在不同的行中。

$date4 = date("Y-m-d"); 
$date3 = date('Y-m-d',strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')))); 


    $sql3 = "SELECT * FROM trn_cal WHERE dat >= '$date4' AND dat <= '$date3'"; 
    $res3 = mysql_query($sql3); 
    echo "<table width='700px' border='1'>"; 
    echo " <tr>"; 
    echo "  <td width='100px'>Sunday</td>"; 
    echo "  <td width='100px'>Monday</td>"; 
    echo "  <td width='100px'>Tuesday</td>"; 
    echo "  <td width='100px'>Wednesday</td>"; 
    echo "  <td width='100px'>Thursday</td>"; 
    echo "  <td width='100px'>Friday</td>"; 
    echo "  <td width='100px'>Saturday</td>"; 
    echo " </tr>"; 
    echo "</table>"; 

    echo "<table width='700px' border='1'>"; 
    while ($row3 = mysql_fetch_assoc($res3)) { 
    $weekday = $row3['weekday']; 
    $weekdate = $row3['weekdate']; 
    echo " <tr>"; 
    echo "  <td width='100px'>"; 
    if ($weekday == "Sunday") { 
    echo $weekdate; 
    } 
    echo "</td>"; 
    echo "  <td width='100px'>"; 
    if ($weekday == "Monday") { 
    echo $weekdate; 
    } 
    echo "</td>"; 
    echo "  <td width='100px'>"; 
    if ($weekday == "Tuesday") { 
    echo $weekdate; 
    } 
    echo "</td>"; 
    echo "  <td width='100px'>"; 
    if ($weekday == "Wednesday") { 
    echo $weekdate; 
    } 
    echo "</td>"; 
    echo "  <td width='100px'>"; 
    if ($weekday == "Thursday") { 
    echo $weekdate; 
    } 
    echo "</td>"; 
    echo "  <td width='100px'>"; 
    if ($weekday == "Friday") { 
    echo $weekdate; 
    } 
    echo "</td>"; 
    echo "  <td width='100px'>"; 
    if ($weekday == "Saturday") { 
    echo $weekdate; 
    } 
    echo "</td>"; 
    echo " </tr>"; 
    } 
    echo "</table>"; 
+0

每次因爲你回聲 「」 通話時間你得到一個不同的行;每次你經歷你的while循環。你可能只想開始一個新的行,如果你在星期天之前 – ioums

+0

@ ioums,嗨,謝謝我理解這個問題,但我無法讓它工作 – Wilest

回答

0

試試這個:

$date4 = date("Y-m-d"); 
$date3 = date('Y-m-d',strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')))); 

$sql3 = "SELECT * FROM trn_cal WHERE dat >= '$date4' AND dat <= '$date3'"; 
$res3 = mysql_query($sql3); 

echo "<table width='700px' border='1'>"; 

echo " <tr>"; 
echo "  <td width='100px'>Sunday</td>"; 
echo "  <td width='100px'>Monday</td>"; 
echo "  <td width='100px'>Tuesday</td>"; 
echo "  <td width='100px'>Wednesday</td>"; 
echo "  <td width='100px'>Thursday</td>"; 
echo "  <td width='100px'>Friday</td>"; 
echo "  <td width='100px'>Saturday</td>"; 
echo " </tr>"; 

echo " <tr>"; 
$results_count = mysql_num_rows($res3); 
$i = 1; 
while ($row3 = mysql_fetch_assoc($res3)) { 
    $weekday = $row3['weekday']; 
    $weekdate = $row3['weekdate']; 
    echo "  <td width='100px'>" . $weekdate . "</td>"; 
    if ($weekday == "Sunday") { 
     echo " </tr>"; 
     if($i < $results_count) echo " <tr>"; 
    } 
    $i++; 
} 

echo "</table>";