[SOLVED]Probably a simple question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

[SOLVED]Probably a simple question

Post by Wldrumstcs »

Ok, I have a bunch of names (first and last) in a db. I also have a page where I output the list of names along with other info about that person. How would I show the output in alphabetical order according to the LAST name. Basically, how would I split up the last names from the first names and then sort alphabetically? Thanks and sorry for my lame questions :)
Last edited by Wldrumstcs on Sun Dec 05, 2004 10:23 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

$query = "select * from names order by lastname asc";
Something like that would do it, not sure if that is exactly right or not. If you have both names in the same column, which I wouldn't recommend, then the query would become much more difficult, if not impossible.
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

Post by Wldrumstcs »

Well, kind of. I know how to display it alphabetically, but my FIRST and LAST names are in one field called "name". I have to split that field up to order it by just the last name.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I would put all the data into an array and the use a php array sort function to sort them then. Do you have a php manual?
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

Post by Wldrumstcs »

nope.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Download the chm version all the way to the right. It's a very nice manual with search features.

http://www.php.net/download-docs.php
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

Post by Wldrumstcs »

Found the answer:

$query="SELECT * FROM teachers SORT BY SUBSTRING_INDEX(username, ' ', -1),SUBSTRING_INDEX(username, ' ',1)";
$result=mysql_query($query);
Post Reply