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
So what's your question? How to get the first/second/third letters of a name? substr($name, location, 1)
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.
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 ?
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.