Jump to content

im not at all sure if this is possible, and if it is, ill be amazed!


dbradbury

Recommended Posts

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?

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); 

<?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.

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.

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!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.