2011-02-08 232 views
0

如何在while循環中放置列表?PHP列表while循環

PHP:

> $q = $db->query("SELECT * FROM 
> bs_events LEFT JOIN bs_reservations ON 
> bs_reservations.id_event = bs_events.id 
> GROUP BY bs_reservations.id_event 
> ORDER BY bs_events.eventDate ASC LIMIT 20"); 

HTML表格:

> while($r = $q->fetch_array(MYSQLI_ASSOC)): 
> echo '<tr>'; 
> echo '<td>' . $r['title'] . '</td>'; 
> echo '<td>' . date('d/M/Y', strtotime($r['eventDate'])) . '/' . $r['eventTime'] . '</td>'; 
> echo '<td>' . $r['location'] . '</td>'; 
> echo '<td>' . array($r['username']) .'</td>'; 
> echo '<td ></td>'; 
> echo '<td ></td>'; 
> echo '</tr>'; 
> endwhile; 

上述代碼將輸出

TITLE  DATE/TIME  LOCATION USERNAME 
================================================= 
1  dd/mm/yyy/hh:mm location username 
1  dd/mm/yyy/hh:mm location username1 
1  dd/mm/yyy/hh:mm location username2 
2  dd/mm/yyy/hh:mm location username 

,我想輸出

TITLE  DATE/TIME  LOCATION USERNAME 
================================================= 
1  dd/mm/yyy/hh:mm location username, username1, username2 
2  dd/mm/yyy/hh:mm location username 

或者我需要把foreach所echo '<td>' . array($r['username']) .'</td>';

回答

2

喜歡的東西

SELECT title, 
     event_date, 
     event_time, 
     location, 
     GROUP_CONCAT(username SEPARATOR ', ') AS username 
    FROM bs_events 
    LEFT JOIN bs_reservations 
     ON bs_reservations.id_event = bs_events.id 
GROUP BY bs_reservations.id_event 
      bs_events.title, 
      bs_events.event_date, 
      bs_events.event_time, 
      bs_events.location 
ORDER BY bs_events.eventDate ASC LIMIT 20 

編輯

新增別名,用戶名

+0

感謝..但其在用戶名上顯示空白。我如何把用戶名列表'$ r ['username']`? – tonoslfx 2011-02-08 17:21:11