Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:44:01+00:00 2026-06-09T04:44:01+00:00

So my website allows users to create a contacts list and now I have

  • 0

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 
            ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:44:04+00:00Added an answer on June 9, 2026 at 4:44 am

    Just also retrieve the category and sort first by category and then by name. And check whether the category is different than the last:

    $contacts_query = "SELECT id, name, category";
    $contacts_query.= " FROM contacts";
    $contacts_query.= " WHERE ownerid = '$userID'";
    $contacts_query.= " ORDER BY `category`, `name` ASC";
    
    $run_contacts_query = mysql_query($contacts_query);
    
    if($run_contacts_query) {
        $currentCategory = null;
    
        while($c_data = mysql_fetch_assoc($run_contacts_query)){
            if ($c_data['category'] != $currentCategory) {
                echo '<li>' . $c_data['category'] . '</li>';
                $currentCategory = $c_data['category'];
            }
    
            echo '<li><a href="contact.php?id=' . $id . '">' . $name . '</a></li>';
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website that allows a user to create blog posts. There are
I have a CMS website written in aspx 2.0 that allows users to build
I have an entries controller that allows users to add contact information the website.
I have a website that allows a user to upload a spreadsheet of items
I have created a website which allows users to sign up for, and use,
I have a website where I allow users to create new Part records. I'm
I'm working on a website that allows users to create an account. One of
On the front-end, I have a PHP webapp that allows users to create a
I am building an ASP.NET website that allows users to create and take tests.
How can I create a list of items that allows users to click each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.