Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.