Another php question - How to change names (first Last) round?

Discussion in 'PHP' started by GloBleeOne, May 28, 2008.

  1. #1
    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
     
    GloBleeOne, May 28, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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..
     
    zerxer, May 28, 2008 IP
  3. dddougal

    dddougal Well-Known Member

    Messages:
    676
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    108
    #3
    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.
     
    dddougal, May 29, 2008 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    zerxer, May 29, 2008 IP
  5. Normac

    Normac Peon

    Messages:
    40
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    Normac, May 29, 2008 IP