Jump to content

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


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.

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.