João João - 6 months ago 19
SQL Question

Merging MySQL data into the same list

Hi I'm trying to do a change log page.
I have the change log content in a database and then I want to display it by date in a PHP page.

Here's the code:

$getChangeLog = "SELECT * FROM changelog ORDER BY id DESC LIMIT 6";
$query = $conn->query($getChangeLog);
echo "<h1>CHANGE LOG</h1>";
while ($row = mysqli_fetch_assoc($query)) {
echo "<h4>".$row["date"]."</h4>";
echo "<ul><li>".$row["change"]."</li></ul>";
}


My SQL Databse:

My SQL databse

I want to display unordered lists with changes that have same dates.

It should look like this:

Example Database Done

Answer
$getChangeLog = "SELECT * FROM changelog ORDER BY date DESC, id DESC LIMIT 6";      
$query = $conn->query($getChangeLog);
echo "<h1>CHANGE LOG</h1>"; 
$c = 0;
while ($row = mysqli_fetch_assoc($query)) { 
    $list[$c] = $row;
    if($row["date"]!=$list[$c-1]["date"])
        echo ($c==0?"":"</ul>")."<h4>".$row["date"]."</h4><ul>";  
    echo "<li>".$row["change"]."</li>";
    $c++;
 }
echo "</ul>";