2012-08-30 33 views
0

我想顯示我的整個表,但按天分組。顯示SQL項目的日子

所以以後我

SELECT * FROM events ORDER BY event_date, event_time 

我想他們要顯示這樣。

<table> 
<th><?php echo date('W M D', strtotime($row->event_date)); ?></th> 
<tr> 
<td>Location Time Event</td> 
</tr> 

<th><?php echo date('W M D', strtotime($row->event_date)); ?></th> 
<tr> 
<td>Location Time Event</td> 
<td>Location Time Event</td> 
</tr> 
</table> 

但我無法在任何地方找到一個例子。只分組和歸類項目。

編輯。我不想添加日期,但按天顯示項目。

實施例輸出

September 8th 
-- Location Time Event 
-- Location Time Event 

September 9th 
-- Location Time Event 

October 10th 
-- Location Time Event 

EDIT 2.

改寫了代碼

<?php 
      require_once("config.php"); 
      $results = $db->get_results('SELECT * FROM events ORDER BY event_date, event_time'); 
      foreach($results as $row){ 
       $row->id // all the items are available as objects 
      } 
      $curr_date = '' 
      foreach($results as $row){ 
      if ($curr_date != $row->event_date){ 
      /* Handle the date’s title here */ 
      printf("<h1>%s</h1>", $row->event_date); 
      $curr_date = $row->date 
      } 
      /* Handle row items here */ 
      printf("<tr><td>%s</td><td>%s</td></tr>", $row->presenter , $row->location); 
      } 
      ?> 

仍然接收錯誤。這裏出現誤差約爲$按行> ID突發T_VARIABLE

+1

您可以遍歷它們,並將其添加到一個數組,如果你想這樣做在PHP。使索引成爲日期。 $ events [$ row-> event_date] [] = $ row;如果他們是相同的日期,他們將被加在一起 – wesside

+0

或者不做多個循環。只需在循環中創建一個if子句並輸出分割內容所需的日期和HTML。 – ThoKra

回答

0

得到它的工作感謝您的幫助!

代碼:

$results = $db->get_results('SELECT * FROM events ORDER BY event_date, event_time'); 
      $curr_date = ''; 
      foreach($results as $row){ 
      if ($curr_date != $row->event_date){ 
      /* Handle the date’s title here */ 
      printf("<h1>%s</h1>", $row->event_date); 
      $curr_date = $row->event_date; 
      } 
      /* Handle row items here */ 
      printf("<tr><td>%s</td><td>%s</td></tr></br>", $row->presenter , $row->location); 
      } 
0

什麼是這樣的:

$result = mysql_query("SELECT * FROM events ORDER BY event_date, event_time"); 
while($row = mysql_fetch_array($result)) { 
    $event_date = $row['event_date']; 
    $location_event = $row['some event']; 
    if ($event_date == $last_date) { 
     echo "<tr><td>$location_event</td></tr>"; 
    } else { 
     echo "<th>$event_date</th>"; 
     echo "<tr><td>$location_event</td><tr>"; 
    } 
    $lastdate = $event_date; 
} 
+0

在您進入While循環之前,我收到了'if($ event_date == $ last_date)'''''DEFINED VARIABLE:LAST_DATE''' – shayward

+0

,以便它在首次通過時有一個值。 –

0

沒有測試過這一點,但它會創建一個多維數組,可以讓你遍歷日期和隨後的時間。

$tempArray = array(); 

$query = "SELECT event_date, event_time FROM events ORDER BY event_date, event_time"; 
$result = mysqli_query($link, $query) or die(mysqli_error($link)); 

while($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { 
    $event_date = $row[event_date]; 
    $event_time = $row[event_time]; 

    $tempArray[$event_date][] = $event_time; 
} 

foreach($tempArray as $k1=>$event_date) { 
    echo "<h1>$event_date</h1>"; 

    foreach($tempArray[$event_date] as $k2=>$event_time) { 
     echo "<p>$event_time</p>"; 
    } 
} 

mysqli_free_result($result); 

unset($tempArray);