Edward Glasser
Edward Glasser

Reputation: 193

PHP/MYSQL populate dropdown sorted by query results

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.

Upvotes: 2

Views: 479

Answers (2)

Erenor Paz
Erenor Paz

Reputation: 3171

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

Upvotes: 1

Joe Green
Joe Green

Reputation: 1777

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

The code in your loop should look like this, and you need to open and close a <ul> from outside the loop.

echo '<ul>';
foreach () { // here is the loop }
echo '</ul>';

THis is doing it the "dirty" way with printing out as you go, at least.

The way I would do it would be to build a 2D array indexed first by group_name and then by user ID. It would mean making two passes at the data as a second loop would be needed to display the data, but generally speaking you're not going to feel the difference.

Upvotes: 1

Related Questions