按日期对 MySQL 查询结果进行分组,每个组都有标题和自己的 div
Posted
技术标签:
【中文标题】按日期对 MySQL 查询结果进行分组,每个组都有标题和自己的 div【英文标题】:Group MySQL query results by date and each group has header and own div 【发布时间】:2012-10-06 03:33:48 【问题描述】:我觉得我可能过度考虑了这个解决方案......所以我想我会得到一些额外的建议。我想在我的数据库中查询在单个标题下具有相同日期的“事件”列表和组事件,此外,我希望这个“组”在它自己的容器中。我在浏览其他问题时发现了单个标题问题,但容器让我很伤心!
例如:
这个:
<div class='container'>
<img src='event-image.jpg'>
<h1>October 15th, 2012</h1>
<h3>Event Name</h3>
<p>Details</p>
<h3>Event Name</h3>
<p>Details</p>
<h3>Event Name</h3>
<p>Details</p>
</div>
<div class='container'>
<img src='event-image.jpg'>
<h3>Event Name</h3>
<p>Details</p>
<h3>Event Name</h3>
<p>Details</p>
<h3>Event Name</h3>
<p>Details</p>
</div>
不是这个:
<div class='container'>
<img src='event-image.jpg'>
<h1>October 15th, 2012</h1>
<h3>Event Name</h3>
<p>Details</p>
</div>
<div class='container'>
<h3>Event Name</h3>
<p>Details</p>
</div>
<div class='container'>
<h3>Event Name</h3>
<p>Details</p>
</div>
<div class='container'>
<img src='event-image.jpg'>
<h3>Event Name</h3>
<p>Details</p>
</div>
<div class='container'>
<h3>Event Name</h3>
<p>Details</p>
</div>
<div class='container'>
<h3>Event Name</h3>
<p>Details</p>
</div>
这个:
October 15, 2012
Event 1
Event 2
Event 3
不是这个:
October 15, 2012
Event 1
October 15, 2012
Event 2
October 15, 2012
Event 3
MySQL 查询:
$sql = "SELECT title, DATE_FORMAT(date, '%M %D, %Y') AS postdate, time, venue, cost, city, spotlight, image
FROM shows WHERE date >= $curDate
ORDER BY date ASC, spotlight DESC LIMIT $startRow," . SHOWMAX;
HTML/PHP:
<?php
$prevDate = null;
while ($row = $result->fetch_assoc()) ?>
<div class="content">
<?php
if (!empty($row['image'])) ?>
<img src="/images/thumbs/<?php echo $row['image'] ?>">
<?php
if ($row['postdate'] != $prevDate) ?>
<h1><?php echo $row['postdate']; ?></h1>
<?php $prevDate = $row['postdate'];
?>
<h3><?php echo $row['title']; ?></h3>
<p><?php echo $row['venue'] . " | " . $row['city'] . " | " . $row['time'] . " | " . $row['cost']; ?></p>
</div>
<div class="horizontal-line"></div>
<?php ?>
结果:
<div class="content">
<img src="/images/thumbs/defining-times-news-00.jpg">
<h1>October 13th, 2012</h1>
<h3>Relient K w/ Hellogoodby, William Beckett & House of Heroes</h3>
<p>Cain's Ballroom | Tulsa, OK | 9:00 | $10</p>
</div>
<div class="horizontal-line"></div>
<div class="content">
<h3>Rod Steward & Stevie Nicks</h3>
<p>BOK Center | Tulsa, OK | 9:00 | $45</p>
</div>
<div class="horizontal-line"></div>
<div class="content">
<h3>Gary Allan</h3>
<p>Hard Rock Hotel & Casino | Tulsa, OK | 9:00 | $30</p>
</div>
<div class="horizontal-line"></div>
<div class="content">
<img src="/images/thumbs/dead-sea-choir-news-00.jpg">
<h1>October 14th, 2012</h1>
<h3>Nalani Proctor and Kierston White</h3>
<p>Bluebonnet | Norman, OK | 9:00 | $5</p>
</div>
<div class="horizontal-line"></div>
<div class="content">
<h3>Bungalouski</h3>
<p>Bluebonnet | Norman, OK | 9:00 | FREE</p>
</div>
<div class="horizontal-line"></div>
<h3>Relient K w/ Hellogoodby, William Beckett & House of Heroes</h3>
<p>Cain's Ballroom | Tulsa, OK | 9:00 | $10</p>
</div>
<div class="horizontal-line"></div>
我希望这不会太复杂!任何帮助表示赞赏!谢谢!
【问题讨论】:
它做错了什么?是横线吗?您的示例没有提到带有class="content"
的项目,所以我无法判断这是否正确。
我以为我的例子做了......但它做错的是在 中包装每个“事件”(H3标签和P标签),然后是水平规则。 ..我希望它用相同的日期包装所有“事件”,然后用一条水平线添加视觉分隔。
【参考方案1】:
感谢 'khomyakoshka' 引导我朝着正确的方向前进!
...现在我并不是说这是最好的方法,但它确实有效!
<?php
$prevDate = null;
$i = 0;
while ($row = $result->fetch_assoc())
$mark = false;
if ($row['postdate'] != $prevDate)
$mark = true;
$prevDate = $row['postdate'];
if ($mark && $i == 0)
?>
<div class="content">
<?php
elseif ($mark)
?>
<div class="clear"></div>
</div>
<div class="horizontal-line"></div>
<div class="content">
<?php
if (!empty($row['image']))
?>
<img src="/images/thumbs/<?= $row['image'] ?>">
<?php
if ($mark) ?>
<h1><?php echo $row['postdate']; ?></h1>
<?php
?>
<h3><?php echo $row['title']; ?></h3>
<p><?php echo $row['venue'] . " | " . $row['city'] . " | " . $row['time'] . " | " . $row['cost']; ?></p>
<?php
$i++;
?>
<div class="clear"></div>
</div>
【讨论】:
我会接受你的回答,因为你确实回答了这个问题,我只需要稍微修改一下以适应我的代码。再次感谢!我会更频繁地使用这个地方。 欢迎来到 *** =)【参考方案2】:试试这个:
$prevDate = null;
while ($row = $result->fetch_assoc())
$mark = false;
if ($row['postdate'] != $prevDate)
$mark = true;
$prevDate = $row['postdate'];
?>
<? if ($mark) ?>
<div class="content">
<? ?>
<? if (!empty($row['image'])) ?>
<img src="/images/thumbs/<?= $row['image'] ?>">
<? ?>
<? if ($mark) ?>
<h1><?= $row['postdate']; ?></h1>
<? ?>
<h3><?= $row['title']; ?></h3>
<p><?= $row['venue'] . " | " . $row['city'] . " | " . $row['time'] . " | " . $row['cost']; ?></p>
<? if ($mark) ?>
</div>
<div class="horizontal-line"></div>
<? ?>
<? ?>
【讨论】:
这更接近了! ...而且我想我现在可以解决了。 请看一个例子here。它可以满足您的需求。以上是关于按日期对 MySQL 查询结果进行分组,每个组都有标题和自己的 div的主要内容,如果未能解决你的问题,请参考以下文章