我試圖讓這項工作24小時,現在感覺就像我接近它!從多重循環的SQL中呈現數據的問題
我想獲取日期(柱:基準):從MySQL,那裏有我的代碼看起來像(制地圖列):
<?php
$sql = "SELECT datum FROM gigs GROUP BY DATE_FORMAT(datum, '%Y') ORDER BY datum DESC";
$result = mysql_query($sql);
while($r = mysql_fetch_array($result)) {
$date = $r['datum'];
$date_new = new DateTime($date);
$year = $date_new->format('Y');
$month = $date_new->format('M');
$day = $date_new->format('d');
$sql2 = "SELECT * FROM gigs WHERE DATE_FORMAT(datum, '%Y') = $year ORDER BY datum ASC";
$result2 = mysql_query($sql2);
echo "<tr height=20px><td></td><td align=center><b>".$year."</b></td></tr>";
echo "<tr><td><b>".$month."</b></td></tr>";
while($r2 = mysql_fetch_array($result2)) {
$date2 = $r2['datum'];
$date_new2 = new DateTime($date2);
$year2 = $date_new2->format('Y');
$month2 = $date_new2->format('M');
$day2 = $date_new2->format('j');
//echo "<b>Month: ".$month."</b>";
//echo "<b>Month2: ".$month2."</b>";
if($month != $month2) {
echo "<tr><td> </td></tr><tr><td><b>".$month2."</b></td></tr>";
}
$month = $month2;
echo "<tr class=giglist><td>".$day2."</td><td>".$r2['plats']."</td></tr>";
}
echo "</td></tr>";
}
?>
這給了我:
2011
Jan
1. Location
2. Location
3. Location
...
Feb
1. Location
2. Location
3. Location
...
...
Dec
1. Location
2. Location
3. Location
...
2010
Dec
Jan
1. Location
2. Location
3. Location
...
Feb
1. Location
2. Location
3. Location
...
...
Dec
1. Location
2. Location
3. Location
...
我和地點不希望額外的12月寫在「2010」...
我希望有人可以幫助,因爲我迷失在我自己的代碼xD謝謝!
這看起來比原始代碼好得多 - 但如果我理解了原始代碼的意圖,那麼您將以錯誤的順序打印出來。您應該按年降序,然後在月份(和日)上升。 – kastermester 2011-01-21 12:42:45