Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help With How To Get First Letter In Names and Check MySQL database.

    Can Anyone provide me a sample code of this simple PHP problem, because I completely suck in PHP.. This is just a very simple question that I really can't answer...


    All I need is to get the first letters of the FIRST NAME, MIDDLE NAME, and LAST NAME.

    For example my name is... JOHN BLACK BROWN

    There are 3 text fields in the page... for the first name, the middle name, and the last name...

    I'll enter JOHN for the first name
    BLACK for the middle name
    BROWN for the last name...


    then, the php script should output

    JBB


    and if, JBB is already available in the database, for example there is a JBB already inside my mysql db... it will select the second letter of the MIDDLE INITIAL and so on...

    the output would be

    JLB

    because L is the second letter of BLACK.. and if JLB is present in the database, the output should by

    JAB


    and so on and so forth... please PHP gurus need help of this simple PHP problem

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    k here is a thought :|

    PHP Code:
    function hasClash($nick) {
        
    $clashes = array('JBB''JLB');
        return 
    in_array($nick$clashes);
    }
    function 
    getNext($ar) {
        
    $first  $ar[0],
        
    $middle $ar[1],
        
    $last   $ar[2],
        
    $f $ar[3],
        
    $m $ar[4],
        
    $l $ar[5];
        return 
    substr($first$f1) . substr($middle$m1) . substr($last$l1);
    }
    function 
    getNick($first$middle$last) {
        
    $policy = array(000);
        
    $p = array($first$middle$last);

        
    $nick getNext( ($p $policy) );
        while ( 
    hasClash($nick) ) {
            
    $policy getNextPolicy($policy);
            if ( 
    $policy[0] < strlen($first) && $policy[1] < strlen($middle) && $policy[2] < strlen($last) ) {
                
    $nick getNext( ($p $policy) );
            } else {
                
    $nick '---';
                break;
            }
        }
        return 
    $nick;
    }
    function 
    getNextPolicy($curr) {
        
    $curr[1]++;
        return 
    $curr;

    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    That's confusing code. You'll probably want to add a comment here and there. Or simplify.

    Though I'd question the need for such code. Why have such an odd nickname process?

    What happens if all the letters are taken (what is past Z)? This won't scale at all.

    My suggestion is to think of a better solution for people that have duplicate initials. (If you even need a nickname like that)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •