Jump to content

unique username generation


chrisuk

Recommended Posts

As part of a system I have developed, usernames are generated based on the first letter of the users forename, plus all of their surname, eg. JBloggs - this is standard at the company.

 

What I am wanting to to is if a username already exists (which is unlikely as it is a small company - but possible nonetheless) is to add a number to the end.

 

I have it working so that if JBloggs already exists and I was to try to add another Joe Bloggs, the username would be JBloggs1.

 

However - that's easy.

 

What I am not too sure about is how I can check to see what the number currently is, so that in the extremely unlikely event that a third username would be the same, it would be JBloggs2 - and so on.

 

How can I go about this?

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/48749-unique-username-generation/
Share on other sites

not tested... but i think it'd work...

<?
$username='JBloggs';
$query=mysql_query("SELECT * FROM `users` WHERE `login`='$username%'");
$row=mysql_fetch_assoc($query);
if(empty($row)){
#add the user
}else{
$lastnum=0;
while($row=mysql_fetch_assoc($query)){
  if(is_numeric($row[login]{strlen($row[login])-1})&&$row[login]{strlen($row[login])-1}>$lastnum) $lastnum=$row[login]{strlen($row[login])-1};
}
$lastnum++;
echo $username.$lastnum;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.