Will sort them alphabetically sure, but will it pair them to the corresponding heading letter?
You would do that when you display the results.
The easiest way would be to index/pivot the data using the first letter when you retrieve the data, storing the rows with the same 1st letter into a sub-array using the 1st letter as the main array index. Then, if you want to display all the heading letters, even if there is no data for it, loop through the letters and access the retrieved data, if any, for that letter. If you only want to display heading letters where there is data, just loop over the retrieved data.
To index/pivot the data, after you execute the single query -
$result = []; // define an array to hold the data
while($row = $selector->fetch(PDO::FETCH_ASSOC))
// if you set the default fetch mode when you make the connection, you don't need to specify it each time
{
$index = ucfirst($row["name"][0]); // get the 1st letter and upper case it
$result[$index][] = $row;
}
To display all letters, even if no data -
foreach(range('A','Z') as $letter)
{
echo "<h3>$letter</h3>" . "<br>";
if(isset($result[$letter]))
{
foreach($result[$letter] as $row)
{
echo $row["name"] . "<br>";
}
}
}
To display only the data that exists -
foreach($result as $letter=>$rows)
{
echo "<h3>$letter</h3>" . "<br>";
foreach($rows as $row)
{
echo $row["name"] . "<br>";
}
}