我试图显示我的整个表格,但按天分组。
所以在我
之后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>
但我无法在任何地方找到一个例子。仅对项目进行分组和汇总。
EDIT。我不想添加日期,而是按日显示项目。
示例输出
September 8th
-- Location Time Event
-- Location Time Event
September 9th
-- Location Time Event
October 10th
-- Location Time Event
编辑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);
}
?>
仍然收到错误。错误出现在这里$ row-&gt; id与意外的T_VARIABLE
答案 0 :(得分: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;
}
答案 1 :(得分: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);
答案 2 :(得分: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);
}