dbradbury Posted January 27, 2010 Share Posted January 27, 2010 i have these posts and the subjects are name like John Smith, and there are quite a few of them, and now i need them in alphabetical order which i can do, its just they need to be by surname. is there anything that can split it into the two separate words, then arrange them by the second one, which would be their surname? Quote Link to comment https://forums.phpfreaks.com/topic/190040-im-not-at-all-sure-if-this-is-possible-and-if-it-is-ill-be-amazed/ Share on other sites More sharing options...
aebstract Posted January 27, 2010 Share Posted January 27, 2010 http://php.net/manual/en/function.explode.php $name = "John Smith"; $splitname = explode(" ", $name); echo $splitname[0]; // John echo $splitname[1]; // Smith Quote Link to comment https://forums.phpfreaks.com/topic/190040-im-not-at-all-sure-if-this-is-possible-and-if-it-is-ill-be-amazed/#findComment-1002624 Share on other sites More sharing options...
taquitosensei Posted January 27, 2010 Share Posted January 27, 2010 It's possible if they don't have a middle initial or prefix. $users=array("John Smith","Jane Doe","Geoff Johns); foreach($users as $user) { $users_exp=explode(" ", $user); $users[$users_exp[1]]=$user; } $users=sort($users); Quote Link to comment https://forums.phpfreaks.com/topic/190040-im-not-at-all-sure-if-this-is-possible-and-if-it-is-ill-be-amazed/#findComment-1002625 Share on other sites More sharing options...
mattal999 Posted January 27, 2010 Share Posted January 27, 2010 <?php $names = array("John Smith", "Jake Dome"); $allNames = array(); foreach($names as $name) { $name = explode(" ", $name); $allNames[] = array($name[0], $name[1]); } sort($allNames); print_r($allNames); ?> Try that (UNTESTED). EDIT: Never mind. taquitosensei beat me to it, and his is better. Quote Link to comment https://forums.phpfreaks.com/topic/190040-im-not-at-all-sure-if-this-is-possible-and-if-it-is-ill-be-amazed/#findComment-1002627 Share on other sites More sharing options...
dbradbury Posted January 27, 2010 Author Share Posted January 27, 2010 now would these work with getting the data from mysql... for example, $row['post_subject']; that would be the name... i have a feeling that it will be kinda awkward to implement that bit into it, wont it.. Quote Link to comment https://forums.phpfreaks.com/topic/190040-im-not-at-all-sure-if-this-is-possible-and-if-it-is-ill-be-amazed/#findComment-1002632 Share on other sites More sharing options...
akitchin Posted January 27, 2010 Share Posted January 27, 2010 if you have a recent version of MySQL, you can do this right in the query: SELECT * FROM table ORDER BY SUBSTRING_INDEX(post_subject, ' ', -1) ASC give that a shot. Quote Link to comment https://forums.phpfreaks.com/topic/190040-im-not-at-all-sure-if-this-is-possible-and-if-it-is-ill-be-amazed/#findComment-1002639 Share on other sites More sharing options...
aebstract Posted January 27, 2010 Share Posted January 27, 2010 if you have a recent version of MySQL, you can do this right in the query: SELECT * FROM table ORDER BY SUBSTRING_INDEX(post_subject, ' ', -1) ASC give that a shot. I figured there was something, was looking around. Neat stuff. If this won't work for you, you COULD run 2 queries, do your first one grabbing unique ids and names, explode them and then run another to get your information and order by surname where id = id# or something along those lines. Quote Link to comment https://forums.phpfreaks.com/topic/190040-im-not-at-all-sure-if-this-is-possible-and-if-it-is-ill-be-amazed/#findComment-1002641 Share on other sites More sharing options...
dbradbury Posted January 27, 2010 Author Share Posted January 27, 2010 if you have a recent version of MySQL, you can do this right in the query: SELECT * FROM table ORDER BY SUBSTRING_INDEX(post_subject, ' ', -1) ASC give that a shot. this worked perfectly thanks!! well it did after i replace 'table' with the actual name of my table lol thanks again!! Quote Link to comment https://forums.phpfreaks.com/topic/190040-im-not-at-all-sure-if-this-is-possible-and-if-it-is-ill-be-amazed/#findComment-1002647 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.