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

  • SEARCH
  • Home
  • 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 8114351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:07:17+00:00 2026-06-06T03:07:17+00:00

What I’d like is to create a new unordered list for each new value

  • 0

What I’d like is to create a new unordered list for each new value in a mysql result column.

My query looks like this and i’m throwing it into a data array:

$connection = dbconnect();

$getusers = "SELECT users.usr_id, users.usr_firstname, users.usr_lastname, user_groups.group_name FROM users INNER JOIN user_groups ON users.usr_group = user_groups.group_id ORDER BY group_name ASC, users.usr_firstname ASC, users.usr_lastname ASC";
$result = mysql_query($getusers);

$data_array = array();
while($data = mysql_fetch_array($result)){
    $data_array[] = $data;
}

Now I need to display that data so that each new user_group is an unordered list, and each row with that same group, comes up as a list item of that unordered list.

it’s very similar to this question ( PHP/MySQL Query Results ), but i’m having trouble getting the closing ul’s in the right place. Here’s my code for outputting, though I know it’s wrong because the li’s aren’t really children of the ul’s.

$previousgroup = "";
foreach($data_array as $users){
    if($users['group_name'] != $previousgroup){
        echo "<ul class=\"group\">" . $users['group_name'] . "</ul>";
    }
    else{
        echo "<li id=\"m" . $users['usr_id'] . "\"><h4>" . $users['usr_firstname'] . " " . $users['usr_lastname'] . ", " . $users['cred_name'] . "</h4></li>";
    }
}

Is there a more efficient way of doing this? Thanks for your help.

  • 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-06T03:07:19+00:00Added an answer on June 6, 2026 at 3:07 am

    You could change the while loop this way:

    $data_array = array();
    while($data = mysql_fetch_array($result)){
        $data_array[$data['group_name']][] = $data;
    }
    

    and you would get an array that contains your group_name’s as index of other arrays which contain your data. To create the list, you could do the following:

    <ul>
      <?php
      foreach ($data_array as $temp_key => $temp_array)
      {
        ?>
        <li>
          <?php echo $temp_key; ?>
          <ul>
            <?php
            foreach ($temp_array as $datum)
            {
              echo ("<li>" . $datum['usr_firstname'] . "</li>";
            }
            ?>
          </ul>
        </li>
        <?php
      }
      ?>
    </ul>
    

    Hope it works for you

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

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.