Jump to content

Archived

This topic is now archived and is closed to further replies.

cmgmyr

Alphabetical Lists

Recommended Posts

1. Sorry for the kinda stupid question, but I have been coding all weekend and I can't really think very well any more...so here it goes.

I have a database of users that I want an alphabetically separated list of...

0-9
-----
name
name

A
-----
name
name

and so on...

What would the best thing to do this with? I was thinking with an array of 0-Z and seeing if the name of the user falls under it, but I don't know.

Any ideas?

Thanks,
-Chris

Share this post


Link to post
Share on other sites
Well when you get data from your database it is in an array. So just use a array sorting function. checking it out here:

http://us3.php.net/manual/en/function.sort.php

Share this post


Link to post
Share on other sites
[quote]So just use a array sorting function[/quote]
You can do the sort in the query. Its defining where the headings are that will cause problems. Maybe something like.....

[code=php:0]
<?php

  $sql = "SELECT * FROM names ORDER BY `name` DESC";
  if ($result = mysql_query($sql)) {
    $header = FALSE;
    while ($row = mysql_fetch_assoc($result)) {
      if (!$header) {
        $header = substr($row['name'],0,1);
        $prevheader = substr($row['name'],0,1);
      }
      if ($header <> $prevheader) {
        echo strtoupper($header)."<br />";
        echo "--------<br />";
        $prevheader = $header;
      }
      echo $row['name']."<br />";
    }
  }

?>
[/code]

Not tested, probably a bit messy, but should give you the idea.

Share this post


Link to post
Share on other sites
Thanks guys I will try that out. Here is my query if that helps any.

[code]$query = "SELECT users.userid, users.name, profile.type, profile.url, profile.url_description, profile.image, profile.image_description,
profile.mp3 FROM users INNER JOIN profile on (users.userid = profile.userid) ORDER BY users.name ASC";[/code]

Thanks,
-Chris

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.