0

如何将列表放入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>';

4

2 回答 2

2

查看GROUP_CONCAT

于 2011-02-08T16:55:02.957 回答
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

编辑

为用户名添加别名

于 2011-02-08T16:59:54.573 回答