douyan1944 2012-10-16 05:05
浏览 73
已采纳

按日期对MySQL查询结果进行分组,每个组都有标题和自己的div

I feel like I may be over-thinking this solution... so I figured I'd get some additional advice. I want to query my database for a list of 'events' and group events that have the same date under a single header in addition, I want this 'group' in it's own container. I've figured out the single header issue browsing other questions, but the container is giving me grief!

For Example:

This:

<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>

Not This:

<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>

This:

October 15, 2012
Event 1
Event 2
Event 3

Not This:

October 15, 2012
Event 1
October 15, 2012
Event 2
October 15, 2012
Event 3

MySQL Query:

$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 } ?>

Results:

<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>

I hope this isn't too convoluted! Any help is appreciated! Thanks!

  • 写回答

2条回答 默认 最新

  • du5591 2012-10-16 05:39
    关注

    Try this:

    $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>
        <? } ?>
    <? } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器