I have my code written to select images from my database and display them, grouped by month. I would like every ‘month of photos’ to be in its own “container” div. I cannot figure out how to do this while in a WHILE LOOP. I think I need a way to tell if the next row of data, that is going to get iterated, is a new month or not. Any suggestions?
My code:
<?php
if (isset($_COOKIE['user_id']))
{
if (!isset($_GET['user_id']))
{
$user_id= $_COOKIE['user_id'];
}
else
{
$user_id= $_GET['user_id'];
}
$connect= mysqli_connect("localhost", "root", "", "si");
$query= "SELECT * FROM posts WHERE user_id= $user_id ORDER BY date DESC";
$result= mysqli_query($connect, $query)
or die('error with query');
$date = "0";
while ($row= mysqli_fetch_array($result)) {
if ($date != $row['date']) {
echo "<p> ".$row['date']."</p><br/>";
$date = $row['date'];
}
echo '<img src="'.$row['picture']. '"/>' . "<br/>";
}
}
?>
Here is how it should look…

For example, the FEB 2012 month should be in a different container div than JAN 2012.
It is quite easy to implement this at the same place where you create the month header.
Then you have to close the last div after the while loop by calling: