按日期(天)对数据进行排序/分组?

发布于 2024-12-11 20:08:05 字数 775 浏览 0 评论 0原文

我正在创建一个广告网站。我想要一个这样的列表

2011 年 10 月 22 日星期六(标题)

产品标题 1-城市 - 100$

产品标题 2-城市 - 74$

产品标题 3-城市 - 500$

2011 年 10 月 23 日星期日(标题)

产品标题 4-城市 - 150$

产品标题 5-城市 - 220$

产品标题6-city - 10$

我的代码完美地从最新到最旧的顺序排列列表,但我想在 while 循环中添加一些内容以打印出标题(以粗体显示),例如(2011 年 10 月 22 日星期六)。 基本上,我想根据列表发布日期的名称对列表进行分组 这是我的代码:

$sql = "SELECT post_id,city, title, price, imgurl FROM md_post WHERE category='$cat' AND city='$city' ORDER BY timeStamp desc";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
    {
echo "<a href='adid.php?id=".$row['post_id']."'>". $row['title']. " - ". $row['price']." - ".$row['city']."</a>";
}

I am creating a website for ads. I want to have a list like this

Saturday 10/22/2011 (header)

Product title 1-city - 100$

Product title 2-city - 74$

Product title 3-city - 500$

Sunday 10/23/2011 (header)

Product title 4-city - 150$

Product title 5-city - 220$

Product title 6-city - 10$

My code is ordering the list perfectly from newest to the oldest but I want to add something to the while loop to print out the headers (which are in bold) like (Saturday 10/22/2011). Basically i want to group the lists under the name of the day they were posted on
here is my code:

$sql = "SELECT post_id,city, title, price, imgurl FROM md_post WHERE category='$cat' AND city='$city' ORDER BY timeStamp desc";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
    {
echo "<a href='adid.php?id=".$row['post_id']."'>". $row['title']. " - ". $row['price']." - ".$row['city']."</a>";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洛阳烟雨空心柳 2024-12-18 20:08:05
  1. 将时间戳添加到您的选择字段中
  2. 引入新变量,该变量
  3. while 循环中保存上次使用的时间戳,检查日期形式上次使用的时间戳是否与当前行不同,如果是,则根据当前行时间戳打印标题,将其分配给最后一个使用时间戳,然后打印其余部分。
  1. add timestamp to your select fields
  2. introduce new variable that holds last used timestamp
  3. in while loop check if date form last used timestamp differs from current row, if so print header based on current row timestamp, assign it to last used timestamp and then print the rest.
虫児飞 2024-12-18 20:08:05

假设timeStamp是unix时间戳:

$sql = "SELECT post_id, city, title, price, imgurl, timeStamp FROM md_post WHERE category='$cat' AND city='$city' ORDER BY timeStamp desc";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
    if (!isset($dateold) || $dateold != date('d',$row['timeStamp']) {
        echo '<strong>'.date('l  m/d/Y',$row['timeStamp']).</strong><br />;
    }
    echo "<a href='adid.php?id=".$row['post_id']."'>". $row['title']. " - ".$row['price']." - ".$row['city']."</a>";
    $dateold = date('d',$row['timeStamp']);
}

当它不是unix时间戳,而是有效时间时,只需将timeStamp的第一次出现更改为UNIX_TIMESTAMP(timeStamp)

Assuming timeStamp is a unix timestamp:

$sql = "SELECT post_id, city, title, price, imgurl, timeStamp FROM md_post WHERE category='$cat' AND city='$city' ORDER BY timeStamp desc";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
    if (!isset($dateold) || $dateold != date('d',$row['timeStamp']) {
        echo '<strong>'.date('l  m/d/Y',$row['timeStamp']).</strong><br />;
    }
    echo "<a href='adid.php?id=".$row['post_id']."'>". $row['title']. " - ".$row['price']." - ".$row['city']."</a>";
    $dateold = date('d',$row['timeStamp']);
}

when it's not a unix timestamp, but a valid time, you can just change the first appearance of timeStamp to UNIX_TIMESTAMP(timeStamp)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文