Jump to content


Photo

Alphabetical Lists


  • Please log in to reply
3 replies to this topic

#1 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 24 July 2006 - 12:25 AM

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

#2 scs

scs
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts
  • LocationUSA

Posted 24 July 2006 - 12:29 AM

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/m...nction.sort.php

#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 24 July 2006 - 12:38 AM

So just use a array sorting function

You can do the sort in the query. Its defining where the headings are that will cause problems. Maybe something like.....

<?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 />";
    }
  }

?>

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

#4 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 24 July 2006 - 12:41 AM

Thanks guys I will try that out. Here is my query if that helps any.

$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";

Thanks,
-Chris




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users