chrisuk Posted April 26, 2007 Share Posted April 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
suzzane2020 Posted April 26, 2007 Share Posted April 26, 2007 i gues u cud avoid te confusion this way: y not add the id of the user table with each of the usrnames ie jbloggs_1 jbloggs_2 this way the username wud always be unique Quote Link to comment Share on other sites More sharing options...
chrisuk Posted April 26, 2007 Author Share Posted April 26, 2007 that's a great idea actually, and will save any lengthy logic.... Think I will just do that, thanks! purely as a matter if interest though...it would still be nice to know how to do my original idea. thanks Quote Link to comment Share on other sites More sharing options...
taith Posted April 26, 2007 Share Posted April 26, 2007 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; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.