Hard to explain, and let me show an example.
$getname = $_POST['getname'];
$i++;
$q = "SELECT username FROM User WHERE username LIKE '".$getname."%'";
$r = mysql_query($q);
if(mysql_num_rows($r) > 0){
while($row = mysql_fetch_object($r)){
if($row->username == $getname){
$uname = $getname.$i;
}else if($row->username == ($getname.$i)){
$uname = $getname.$i+1;
}
$i++;
}
}
else
{
$uname = "foo";
}