I have the following data in my database:
initial | word
---------------------------
E | Example1
E | Example2
N | Nextexample1
The desired output:
E
Example1
Example2
N
Nextexample1
So for each initial, it should display that initial and all the words starting with that initial. Important for me: each word should be displayed in a separate div so no GROUP_CONCAT(wordSEPARATOR ', ') suggestions please.
Output at this moment:
E
Example1
E
Example2
N
Nextexample1
My code at this moment:
<?php
$pdo = new PDO('mysql:host=...');
$sql = "SELECT DISTINCT initial, word FROM `exampletable` ORDER by initial ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute();
if($stmtrecords = $stmt->fetchall(PDO::FETCH_ASSOC))
{
foreach($stmtrecords as $result)
{
?>
<div>
<?php echo $result['initial'];?>
</div>
<br>
<div>
<?php echo $result['word'];?>
</div>
<br><br>
The question:
Obviously I need an extra loop to solve this problem but I don't know how to do that exactly?