好的,
因此,我有一个包含各种列的表,其中之一是日期列,可用于对数据库的最新条目进行排序,例如:SELECT * FROM foo ORDER BY date DESC LIMIT 0,25
。这给了我25个最新条目。
我想要的是将一个日期的所有条目组合到html中,这样输出将是:
<li class="date">Date 1.2.2014</li>
<li>Some Entry</li>
<li>Some Entry</li>
<li>Some Entry</li>
<li>Some Entry</li>
<li class="date">Date 1.1.2014</li>
<li>Some Entry</li>
<li>Some Entry</li>
该容器是无关紧要的,他们可以<li>
,<td>
什么的。
我想知道我是否可以通过某种查询在MYSQL中完成此操作,或者需要哪种PHP逻辑以这种方式获取结果。
另外,它应该具有可伸缩性,换句话说,如果我想要最新的50、100、1000条记录,则只需更改LIMIT
查询范围即可。输出将自动“解析”我猜出的结果,并在每次遇到新日期时添加一个日期标题。
谢谢,
只需跟踪日期即可。如果更改,则输出新日期。
// The current date. Start with nothing.
$currentDate = '';
// Loop through your db results
while ($row = mysql_fetch_assoc($result)) {
// Check to see if the date of the current row is the same as previous row.
if ($currentDate !== $row['yourDateCol']) {
// echo out date
echo '<li class="date">'.$row['yourDateCol'].'</li>';
// save the current date in $currentDate to cehck in next loop iteration
$currentDate = $row['yourDateCol'];
}
// echo out event details
echo '<li>'.$row['yourEventCol'].'</li>';
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句