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.

Need a little help with PHP...

Discussion in 'PHP' started by jehzlau, Jan 7, 2008.

  1. #1
    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 :(
     
    jehzlau, Jan 7, 2008 IP
  2. hostingcoupon

    hostingcoupon Peon

    Messages:
    447
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    So what's your question? How to get the first/second/third letters of a name?

    substr($name, location, 1)
     
    hostingcoupon, Jan 7, 2008 IP
  3. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #3
    Hope it will help you
    <?php
    $con = mysql_connect('localhost','user','pass');
    mysql_select_db('dbname');
    
    //$pos2 for middle name,$pos3 for last name	
    function getname($pos2,$pos3)
    {	
    	//thinking you will use post method in form
    	$first =  $_POST['first_name'];
    	$middle = $_POST['middle_name'];
    	$last = $_POST['last_name'];	
    	
    	$name = substr($first,0,1).substr($middle,$pos2,1).substr($last,$pos3,1);	
    	
    	$result = mysql_query("select * from name where nome='$name'");
    	if (mysql_num_rows($result)>0) 
    	{
    		if ($pos2 < (strlen($middle)-1))
    			getname(($pos2+1),$pos3);					
    		else 
    			getname($pos2,($pos3+1));
    		
    	}
    	else
    	{ 
    		//here I echo the $name as result.you do whatever you want with the $name
    		echo 'The output is '.$name;
    	}
    }
    //calling the function here to start calculation from the first letter of the middle name and last name
     getname(0,0);
    
    mysql_close();
    ?>
    PHP:
    EDIT:A slight change for the database connecting stuffs.
     
    coderbari, Jan 7, 2008 IP
  4. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #4
    hey codebari.. this is exactly what i'm trying to find! thank you so much! you rock!! ^_^
     
    jehzlau, Jan 8, 2008 IP
  5. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #5
    by the way codebari.. i have a follow up question.. how can i do this..

    after checking that the INITIAL is available.. for example JAB is available for JOHN BLACK BROWN... it will then be saved in a database... the structure is this

    username | first_name | middle_name | last_name
    JAB | JOHN | BLACK | BROWN


    how can i do it ? :(
     
    jehzlau, Jan 8, 2008 IP
  6. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #6
    I wrote a comment :
    you run a mysql_query to insert them in the table like:

    mysql_query("insert into tablename values ('$name','$first','$middle','$last')");

    You can put this line in place of this line if you want to see the output

    echo 'The output is '.$name;

    or below this line if you want to see the output.
    It is my pleasure that my code helped you. :D
     
    coderbari, Jan 8, 2008 IP
    jehzlau likes this.
  7. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #7
    thanks coderbari ^__^ ur the man!!!
     
    jehzlau, Jan 10, 2008 IP