I have the following code in www.photosales.co.nz/photog_list.php <?php if($n != '1'){ $photogl_result = mysql_query("SELECT * FROM photographers where status = '1' and name LIKE '$n%' order by professional Desc, name", $db); }else{ $photogl_result = mysql_query("SELECT * FROM photographers where status = '1' and professional = '1' order by name", $db); } $photogl_rows = mysql_num_rows($photogl_result); if($photogl_rows > 1){ while($photogl = mysql_fetch_object($photogl_result)){ ?> PHP: Now, when it displays the names, it is sorted by first name, not last name. I have told that this is strange, and it should be sorted by last name. How would I do this? Biggest problem I can see is that the name is store as one eg: Joe Bloggs
Well.. I'd honestly say the easiest thing to do is to just create a separate column named 'last_name' and rename your 'name' column to 'first_name' and then run a script to split the original name column and store the values into the other two. Then you'd just sort by last_name. I really don't know if there's a way to make the query split the column's value and sort by the 2nd half..
Or split the first and last name and load them into an array, then sort the array by that column. If you create another column in your mysql database instead then i would still have just one entry on the input form and split the names with php before you insert them, people are lazy and will begrudge having to type in two forms.
What's hard about having to type in two input boxes? Just a simple tab jumps to the next which would be the same as pressing space bar otherwise. At least with two boxes he could make sure they actually entered two proper names easier.
Your problem is the name variable is only in one part. When it sorts out an array it does it from the first letter. This is so there is no mistakes. Take for example the name "Normac David Wal" Does it sort by Normac, David or Wal? Instead you want to do something like (Off top of head) $full_name=explode("$n", " "); if (!$full_name[2]) //check if person didn't input more then 2 names. { $first_name=$full_name[0]; $last_name=$full_name[1]; //Do insert into database here. } else { //redirect to form page again, possibly add error that person added more then 2 names. } ?> Code (markup): Then edit the above code and the tables to include sorting via the $last_name variable.