I'm trying to sort a list of names with allocated links coming from mysql table.
Would like it to look something like this in html:
<h3>A</h3>
<ul>
<li>Andrea</li>
<li>Arron</li>
</ul>
So far I was able to list letters and names in alphabetical order, but I don't know how to make a loop that would list all names starting with 'A' under heading 'A' etc.
<?php
$connection = mysqli_connect('localhost', 'root', '', 'database');
$query = "SELECT * FROM users ORDER BY name";
$result = $connection->query($query);
$azRange = range('A', 'Z');
foreach ($azRange as $letter){
echo "<h3>" . $letter . "</h3>";
}
echo "<ul>";
while($row = $result->fetch_array()){
echo "<li><a href=\"" . $row['link'] . "\">" . $row['name'] . "</a></li>";
}
echo "</ul>";
?>
Thank you.