Jason Soza wrote:
> I have a 'name' field in a MySQL table that contains people's first and 
> last names, i.e. "John Smith" - but some people may choose not to 
> include their last name, so they're just "John" in the table, or they 
> may choose to include themselves and someone else, but not the last 
> name, "John and Jane".
> I'm thinking the easiest way to sort by last name, which is what I want 
> to do, is to just go into MySQL and make 'first_name' and 'last_name' 
> fields, but if there's a way to do this with PHP that'd be great, so 
> then I wouldn't have to mess with the table and data.
> Anyhow, I've looked up sort() and asort() in the PHP manual, but I'm 
> not sure how I'd go about this. I assume I'd use some kind of function 
> to read the 'name' field to the first space, take what it finds after 
> the space and put it into an array, then have some if/else statement to 
> deal with the lack of a space or the presence of multiple spaces, then 
> use sort() on that array. 
> Just looking for some guidance, maybe a specific function or bit of 
> code. Anything that'd help. Or if this would be more easily addressed 
> by reconfiguring my MySQL table, just let me know.
> Thanks,
> Jason Soza

via MySQL you can search by and order/sort by a substring'ed result.  Of 
course, when someone only puts one name or they enter multiple first 
names, it doesn't work quite the way it's supposed to.

For what i do, the following query returns the first letter of the last 
name in alphabetical order.

SELECT substring(substring_index(fullname,' ',-1),1,1)
FROM user
GROUP BY substring(substring_index(fullname,' ',-1),1,1)
ORDER by substring_index(fullname,' ',-1) ASC

There is a demo at http://www.defconzero.com/user.php?op=view

Hope that provides some insight.

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to