So my website allows users to create a contacts list and now I have added user-named categories. I am currently displaying the list of contacts using a while loop of the associative array generated from my SQL query.
So it looks like this:
Contacts:
Contact 1
Contact 2
Contact 3
Now I have a new column in the database for categories of contacts, and I cannot figure out how to order them by, and display the name of the contact category. I am trying to get it to look like this:
Contacts:
Category 1
Contact 1
Contact 2
Contact 3
Category 2
Contact 1
Contact 2
Contact 3
My actual code if you need it:
<?php
//START CONTACTS LOOP
$contacts_query = "SELECT id, name FROM contacts WHERE ownerid = '$userID' ORDER BY `name` ASC";
$run_contacts_query = mysql_query($contacts_query);
if($run_contacts_query){
while($c_data = mysql_fetch_assoc($run_contacts_query)){
$id = $c_data['id'];
$name = $c_data['name'];
?>
<li><a href="contact.php?id=<?=$id?>"><?=$name?></a></li>
<?php
}} //END CONTACTS LOOP
?>
Just also retrieve the category and sort first by category and then by name. And check whether the category is different than the last: