I have a MySQL query that returns a number of records. For example:
+-------+------------+-----------+
| group | first_name | last_name |
+-------+------------+-----------+
| Red | John | Doe |
+-------+------------+-----------+
| Red | Jane | Doe |
+-------+------------+-----------+
| Green | Bob | Anybody |
+-------+------------+-----------+
| Black | Betty | Anybody |
+-------+------------+-----------+
I also have defined several group names in a PHP array:
$importantGroups = array('Red', 'Blue', 'Green');
Using the query results, I'm trying to write a PHP script that will create an unordered HTML list for each group that's defined in the array.
- If a
groupdoesn't appear in any query results, then that list doesn't get created. - If a result has
groupvalue that doesn't appear in the array, it's placed in a ul at the end.
Using the query results above, the HTML output would be:
<ul id="Red">
<li>John Doe</li>
<li>Jane Doe</li>
</ul>
<ul id="Green">
<li>Bob Anybody</li>
</ul>
<ul id="Other">
<li>Betty Anybody</li>
</ul>
Any help on the best way to make this work?