MYSQL PHP按日期排序,并在每个日期将结果分成组 [英] MYSQL PHP Order By Date and separate results into groups per each date

查看:113
本文介绍了MYSQL PHP按日期排序,并在每个日期将结果分成组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图寻找解决问题的方法,但是我不确定自己要寻找的是什么,所以运气不太好.

I have tried to search for a solution to my problem, but I'm not really sure what I am searching for, so I haven't had much luck.

我有一个简单的MySQL数据库,其中包含一个称为活动"的表.在该表中,我有开始日期",活动描述"和活动位置"的字段.

I have a simple MySQL database with one table called "activities". In that table I have fields for "start_date", "activity_description", and "activity_location".

我正在尝试使用PHP进行MySQL查询,并按升序显示结果,但所有属于同一日期的活动均以标题隔开.

I am trying to do an MySQL query using PHP and display the results in ascending date order, but have all the activities that fall on the same date separated by a heading.

例如,我正在尝试实现以下目标.

For example I am trying to achieve the following.

2012-05-03

  • 带狗散步
  • 公园

  • 出去吃饭
  • 小意大利餐厅
  • Take dog for walk
  • Park

  • Go out for dinner
  • Little Italian Restaurant

2012-05-04

  • 送修汽车
  • 约翰斯汽车与机械公司

2012-05-05

  • 去杂货店购物
  • 新鲜超市

  • 去看电影
  • MegaPlex电影院

  • 与Sam见面
  • 挂在墙上的栏上
  • Do Grocery Shopping
  • Fresh Supermarket

  • Go See Movie
  • MegaPlex Cinemas

  • Meet Up With Sam
  • Hole In The Wall Bar

到目前为止,我已经弄清楚了MQSQL查询必须是这样的:

So far I have worked out that the MQSQL query needs to be something like this:

$result = mysql_query("SELECT * FROM activities ORDER BY start_date ASC")

然后显示结果,我需要这样做:

And then to display the results I need to do this:

while($row = mysql_fetch_array($result))
{
echo 
  '<strong>' .  
  $row['start_date'] . 
  '</strong>' .
  '<ul><li>' . 
  $row['activity_description'] .
  '</li><li>' .     
  $row['activity_location'] . 
  '</li></ul>'; 
 }              

这样给出结果,为每个结果重复日期:

Which gives the results like so, repeating the date for each result:

2012-05-03

  • 带狗散步
  • 公园

2012-05-03

  • 出去吃饭
  • 小意大利餐厅

2012-05-04

  • 送修汽车
  • 约翰斯汽车与机械公司

2012-05-05

  • 去杂货店购物
  • 新鲜超市

2012-05-05

  • 去看电影
  • MegaPlex电影院

2012-05-05

  • 与Sam见面
  • 挂在墙上的酒吧

任何人都可以给我一些提示,提示如何仅对特定日期回显一次"start_date",然后在该日期与上一个日期不同时再次回显吗?

Could anyone give me some tips on how to only echo the 'start_date' once for a particular date, and then echo it again when the date is different from the previous date?

任何技巧,无论多么神秘,都将不胜感激.谢谢.

Any tips, no matter how cryptic would be greatly appreciated. Thanks.

推荐答案

只需跟踪日期即可.

您定义的日期不能像过去的日期或只是假的.

You define a date with something that would not appear like a date in the past or just false.

在lis中运行时,仅在日期更改时打印标题并保存新日期.就是这样:

Running through the lis you only print your heading when the date has changed and save the new date. Just like this:

$currentDate = false;
while($row = mysql_fetch_array($result))
{
if ($row['start_date'] != $currentDate){
 echo
  '<strong>' .  
  $row['start_date'] . 
  '</strong>' ;
  $currentDate = $row['start_date'];
}
 echo 
  '<ul><li>' . 
  $row['activity_description'] .
  '</li><li>' .     
  $row['activity_location'] . 
  '</li></ul>'; 
 }  

关于, 史蒂芬

这篇关于MYSQL PHP按日期排序,并在每个日期将结果分成组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆