MYSQL PHP Order By Date and individual group results for each date

I tried to find a solution to my problem, but I'm not quite sure what I’m looking for, so I’m out of luck.

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

I am trying to execute a MySQL query using PHP and display the results in ascending order of date, but all actions that drop out on the same date, separated by a header.

For example, I am trying to achieve the following.

2012-05-03

  • Take your dog for a walk.
  • A park

  • Go out for dinner
  • Small italian restaurant

2012-05-04

  • Get car service
  • Johns Auto and Mechanical

2012-05-05

  • Shop in stores.


  • Go
  • MegaPlex

  • .

, MQSQL :

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

, , :

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

, :

2012-05-03

  • .

2012-05-03

2012-05-04

  • Johns Auto and Mechanical

2012-05-05

  • .

2012-05-05

  • Go
  • MegaPlex

2012-05-05

  • .

- , "start_date" , , ?

, . .

+5
6

.

-, false.

lis , , . :

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

,

+3

.

<?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>
+2

try this might 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'];     
}  
0
source
$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>';
    }
} 
0
source

Put the date in the $ date variable and check each line 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>'; 

}

0
source

Try something like this (I missed a few style additions to make it a little 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;
0
source

All Articles