I want to select every photo from my database, then display them grouped by month. I have that code here:

```
<?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/>";
    }
}
?>

```

The only problem is that my code displays EVERY image from each month, this makes for an excessively large page. I want to limit my results to only 8 per month (or less if there isn't event 8 pictures for that month), then display a "show more..." link if there are more than 8. I know how to add a LIMIT to my initial query, but that won't work for this scenario. What can I do?

Here is how I want it to look:
![desired layout](https://i.stack.imgur.com/E5G82.jpg)