Page 1 of 1
[SOLVED]Probably a simple question
Posted: Sun Dec 05, 2004 7:45 pm
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

Posted: Sun Dec 05, 2004 8:00 pm
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.
Posted: Sun Dec 05, 2004 8:02 pm
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.
Posted: Sun Dec 05, 2004 8:12 pm
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?
Posted: Sun Dec 05, 2004 8:28 pm
by Wldrumstcs
nope.
Posted: Sun Dec 05, 2004 8:33 pm
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
Posted: Sun Dec 05, 2004 10:23 pm
by Wldrumstcs
Found the answer:
$query="SELECT * FROM teachers SORT BY SUBSTRING_INDEX(username, ' ', -1),SUBSTRING_INDEX(username, ' ',1)";
$result=mysql_query($query);