1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

arranging words in that are in 1 column, possible?

Discussion in 'PHP' started by edual200, May 20, 2009.

  1. #1
    Heres the situation, i have a database of thousands of people names

    some there listed in different ways ( in example below Last is there last name, M is there middle name and First is there first name but all rows are actually different names )

    row1= Last M First
    row2= Last M. First
    row3= Last Middle First
    row4= Last M- First

    what i want to do if possible is use something like str replace that would move First to the begining so i would echo something like
    First Last M
     
    edual200, May 20, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    If they all have a middle name, you can split the string into three parts.

    While looping through the results.

    
    
    while($array = mysql_fetch_array($query))
    {
    
    $name = explode(' ',$array);
    
    $first = $name[2];
    $middle = $name[1];
    $last = $name[0];
    
    }
    
    
    PHP:
    If there is sometimes no middle name, or multiple spaces, you need to use preg_split and some logic to break everything out correctly.
     
    jestep, May 20, 2009 IP
    edual200 likes this.
  3. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #3
    please take note that names need to be consistent for the above method to work, any extra space will defeat the whole logic
     
    creativeGenius, May 20, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    If there's the posibility of multiple spaces, or no middle name, you need something like this:

    
    while(.....):
    
    $name = preg_split("/[\s]+/", $array);
    
    switch(count($name)):
    
    case 3:
    
    $first = $name[2];
    $middle = $name[1];
    $last = $name[0];
    
    break;
    
    case 2:
    
    $first = $name[1];
    $last = $name[0];
    
    break;
    
    endswitch;
    
    
    endwhile;
    
    
    PHP:
     
    jestep, May 20, 2009 IP
  5. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #5
    call the rows out of the database ..
    change the row names to:
    first_name | middle_name | last_name

    edit your database name rows so it works like you want it to.

    then change the $row['first_name'] etc . into PHP vars.

    $first_name = $row['first_name'];
    $middle_name = $row['middle_name'];
    $last_name = $row['last_name'];

    make it do exactly what you want it to do with no confusion. Then you wont have to chang the tire in the rain on a muddy road. :D
     
    ezprint2008, May 21, 2009 IP