I have an array of States, and what I'm trying to do is categorize them by first letter and then display the groups. I have it almost working properly, there's one final problem:
Instead of each state being in it's own gray box, I want all of the states in a single box separated by comma. This is my code:
<?
$lastChar = '';
sort($state_list, SORT_STRING | SORT_FLAG_CASE);
foreach ($state_list as $state) {
$char = $state[0];
echo "<div class=\"stateTable\">";
if ($char !== $lastChar) {
echo "<div class=\"stateTopRow\">" . strtoupper($char) . "</div>";
echo "<div class=\"clear\"></div>";
$lastChar = $char;
}
echo "<div class=\"stateBody\">" . $state . "</div>";
echo "<div class=\"clear\"></div>";
echo "</div>";
}
?>
The issue is that it's looping the entire stateBody div for each individual state. I've tried somehow including stateBody within the if statement, but that breaks the styling entirely (and rightfully so).
Is there some way to include stateBody within the if statement and then add another foreach to loop through the corresponding states? Thank you!