MYSQL PHP Order By Date and separate results into

2020-06-18 09:33发布

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.

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

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

  • Get Car Serviced
  • Johns Auto and Mechanical

2012-05-05

  • Do Grocery Shopping
  • Fresh Supermarket

  • Go See Movie
  • MegaPlex Cinemas

  • Meet Up With Sam
  • Hole In The Wall Bar

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

  • Take dog for walk
  • Park

2012-05-03

  • Go out for dinner
  • Little Italian Restaurant

2012-05-04

  • Get Car Serviced
  • Johns Auto and Mechanical

2012-05-05

  • Do Grocery Shopping
  • Fresh Supermarket

2012-05-05

  • Go See Movie
  • MegaPlex Cinemas

2012-05-05

  • Meet Up With Sam
  • Hole In The Wall Bar

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.

标签: php mysql
6条回答
甜甜的少女心
2楼-- · 2020-06-18 09:53

try this, may work:

$last_date = '';    
while($row = mysql_fetch_array($result))
    {
$display_date = ($last_date == $row['start_date']) ? '' : $row['start_date'];
    echo 
      '<strong>' .  
      $display_date . 
      '</strong>' .
      '<ul><li>' . 
      $row['activity_description'] .
      '</li><li>' .     
      $row['activity_location'] . 
      '</li></ul>'; 
$last_date = $row['start_date'];     
}  
查看更多
Root(大扎)
3楼-- · 2020-06-18 09:57

It can be done like this.

<?php

$data = array();

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

while($row = mysql_fetch_array($result))
{
    if(isset($data[$row['start_date']]))
    {
        $data[$row['start_date']][] = $row;
    }
    else
    {
        $data[$row['start_date']] = array($row);
    }
}

?>

<ul>
    <?php foreach($data as $date => $rows): ?>

    <li>
        <strong><?php echo $date; ?></strong>
        <ul>
            <?php foreach($rows as $row): ?>
                <li><?php echo $row['activity_description']; ?></li>
            <?php endforeach; ?>
        </ul>
    </li>

    <?php endforeach; ?>
</ul>
查看更多
疯言疯语
4楼-- · 2020-06-18 10:03

Just track the date.

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

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>'; 
 }  

Regards, STEFAN

查看更多
\"骚年 ilove
5楼-- · 2020-06-18 10:05

Try something like this (I've left out a few style additions to make it slightly clearer):

$yourdate = '';
$output = '';

while($row = mysql_fetch_array($result)) {
    if ($yourdate != $row['start_date']) {
        $output .= $row['start_date'];
    }

    $output .= '<ul><li>' . 
       $row['activity_description'] .
      '</li><li>' .     
      $row['activity_location'] . 
      '</li></ul>';
}

echo $output;
查看更多
▲ chillily
6楼-- · 2020-06-18 10:13

Put the date in a variable $date and check for each row if the date has changed.

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

}

查看更多
虎瘦雄心在
7楼-- · 2020-06-18 10:18
$result = array();

while($row = mysql_fetch_array($result))
{
    if (empty($result[$row['start_date']]))
    {
        $result[$row['start_date']] = array(); 
    }
    $result[$row['start_date']][] = array($row['activity_description'], $row['activity_location']);
}

foreach($result as $key=>$value)
{
    echo 
       '<strong>' .  
       $key . 
       '</strong>';
    foreach($value as $act)
    {
         '<ul><li>' . 
         $act[0] .
         '</li><li>' .     
         $act[1] . 
         '</li></ul>';
    }
} 
查看更多
登录 后发表回答