...

View Full Version : Help With How To Get First Letter In Names and Check MySQL database.



jehzeel
01-08-2008, 07:23 AM
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

shyam
01-08-2008, 02:34 PM
k here is a thought :|


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, $f, 1) . substr($middle, $m, 1) . substr($last, $l, 1);
}
function getNick($first, $middle, $last) {
$policy = array(0, 0, 0);
$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;
}

aedrin
01-08-2008, 04:42 PM
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)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum