2011-01-21 51 views
0

我試圖讓這項工作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>&nbsp;</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謝謝!

回答

1

你的代碼是多複雜,你能做到這一點,只有一個查詢,查看此代碼

<?php 
$sql = 'SELECT 
      *, 
      DATE_FORMAT(datum, "%Y") "year", 
      DATE_FORMAT(datum, "%M") "month", 
      DATE_FORMAT(datum, "%e") "day" 
     FROM gigs 
     ORDER BY year DESC, month DESC, day ASC'; 

$year = null; 
$month = null; 
$stmt = mysql_query($sql); 
while($row = mysql_fetch_array($result)) { 
    if ($year != $result['year']) { 
     // Year changed 
     $year = $result['year']; 
     echo "<tr height=20px><td></td><td align=center><b>".$year."</b></td></tr>"; 
    } 
    if ($month != $result['month']) { 
     // Month changed 
     $month = $result['month']; 
     echo "<tr><td>&nbsp;</td></tr><tr><td><b>".$month."</b></td></tr>"; 
    } 
    echo "<tr class=giglist><td>".$result['day']."</td><td>".$row['plats']."</td></tr>"; 
} 
+0

這看起來比原始代碼好得多 - 但如果我理解了原始代碼的意圖,那麼您將以錯誤的順序打印出來。您應該按年降序,然後在月份(和日)上升。 – kastermester 2011-01-21 12:42:45

0

首先請拜託,我真的推薦你使用分離關注原則!使用模板系統,例如Smarty,http://www.smarty.net/並使用數據庫抽象層。那麼你的代碼將更加靈活,更易於閱讀等等,然後你就不需要問這個問題,並且節省很多很多時間。

這就是說,你可以嘗試這個(雖然我沒有測試它..);

<?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>"; 
    $first_month_printed = false; 

    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'); 

     if($month != $month2) { 
      echo "<tr><td>&nbsp;</td></tr><tr><td><b>".$month2."</b></td></tr>"; 
     } else if (!$first_month_printed) 
      echo "<tr><td>&nbsp;</td></tr><tr><td><b>".$month2."</b></td></tr>"; 
      $first_month_printed = true; 
     } 

     $month = $month2; 
     echo "<tr class=giglist><td>".$day2."</td><td>".$r2['plats']."</td></tr>"; 
    } 

    echo "</td></tr>"; 
} 

?> 
+0

謝謝!我想我會堅持Xavier的代碼,因爲它更有效(少char)! – Robin 2011-01-22 04:02:12