Jump to content

Alphabetical Lists


cmgmyr

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
Link to comment
https://forums.phpfreaks.com/topic/15439-alphabetical-lists/
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.
Link to comment
https://forums.phpfreaks.com/topic/15439-alphabetical-lists/#findComment-62586
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
Link to comment
https://forums.phpfreaks.com/topic/15439-alphabetical-lists/#findComment-62588
Share on other sites

Archived

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

×
×
  • Create New...

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.