Group dates in a foreach loop

前端 未结 3 714
日久生厌
日久生厌 2021-01-15 04:54

I want to add a new line in order to group dates within a foreach loop.

Example:

2015-11-05
2015-11-05

2015-11-07

2015-11-10
2015-11-1         


        
3条回答
  •  春和景丽
    2021-01-15 05:46

    Both answers provided by Mureinik and Subin are good. But you also have the option to do the following

    1. Edit your query to something like this
    SELECT datetime_published,count(*) as count,`id_visitor`,`id_category`,`datetime_published`,`datetime_edited`,`data_content` FROM table group by `datetime_published` order by `datetime_published` desc
    
    1. Now you have an array with the dates and the count it should print these dates. Then you can print it with a 'for loop'
    for ($i = 1; $i <= $row['count']; $i++) {
    echo $row['datetime_published']." ".$row['data_content'] .'
    '; } echo "
    ";

    Complete Solution spoiler

    ";
            if ($results=mysqli_query($con,$var))
            {
                // Fetch one and one row
                while ($rows=mysqli_fetch_assoc($results))
                {
                print $rows['data_subject']. " " . $rows['data_content'];
                echo "
    "; } } echo "
    "; } } echo "
    "; // Free result set mysqli_free_result($result); mysqli_close($con); ?>

提交回复
热议问题