This is the code i have by now:
<?php
$sql = DB::query("SELECT name FROM stores ORDER BY name");
foreach ($sql as $row){ $name = $row["name"]; }
// don't know how to get the current letter
$current_letter = '?';
// Set the start at 1
$i = 1;
foreach ($sql as $row){
// If the loop hits 1, add <div>
if($i == 1)
echo '<div class="store-wrapper">'.PHP_EOL;
echo '<div class="store-letter">Store Letter - '. $current_letter .'</div>'.PHP_EOL;
echo '<ul>'.PHP_EOL;
echo $id = "\t<li>".$name.'</li>'.PHP_EOL;
// If the loop hits 3, add </div>
if($i == 3) {
echo '</ul>'.PHP_EOL;
echo '</div>'.PHP_EOL; // end .store-wrapper
// Reset the counter
$i = 0;
}
// Increment up
$i++;
}
?>
Is there something wrong with my code? This is the code output that i am trying to achieve:
<div class="store-wrapper">
<div class="store-letter">Store Letter - A</div>
<ul>
<li>ajhgjgj</li>
<li>abgjkhjh</li>
<li>avbnvnmnm</li>
</ul>
</div>
<div class="store-wrapper">
<div class="store-letter">Store Letter - B</div>
<ul>
<li>bgfghfj</li>
<li>bhgjhgj</li>
<li>bvkjhgkj</li>
</ul>
</div>
<div class="store-wrapper">
<div class="store-letter">Store Letter - C</div>
<ul>
<li>cgfghfj</li>
<li>chgjhgj</li>
<li>cvkjhgkj</li>
</ul>
</div>
Another thing is that i don't really know how to get the first letter of each group.
The problem with the code is that the output does not display as the upper example and the groups don't jumb to the next available letter when maximum 3 <li> are reached.
What i am doing wrong?
foreachloop just keeps overwriting the$namevariable. After the loop it contains the name from the last row.$row['name'][0].