0

I'm using the first & last name in the user registration form to generate the username for the new user:

$firstname = $form_state['values']['field_firstname'][LANGUAGE_NONE][0]['value'];
$lastname = $form_state['values']['field_lastname'][LANGUAGE_NONE][0]['value'];
$username = $firstname.''.$lastname;
$result = db_query("SELECT COUNT(*) FROM {users} WHERE name = :name", array(':name' => $username));
$results_number = $result->rowCount();
if ($results_number == 0) {
    $form_state['values']['name'] = $username;
}
else {
    $i = 1;
    $username = $firstname.''.$lastname.''.$i;
    while ($results_number > 0) {
        $result = db_query("SELECT COUNT(*) FROM {users} WHERE name = :name", array(':name' => $username));
        $results_number = $result->rowCount();
        $i++;
        $username = $firstname.''.$lastname.''.$i;
        $form_state['values']['name'] = $username;
    }
}

But I get an infinite loop. There is a problem with that while loop, any suggestions on the best way to fix that?

2
  • rowCount() wont work with SELECT COUNT(*). And Drupal user name is unique. With out LIKE statement if you are fetching value with user name, always you will get Row Count value is 1.
    – Kalidasan
    Jan 24 '14 at 14:28
  • the count part I understand, I must have missed it in some terrible way - the 2nd part should work though, $i is increased every time the number of results is > 0, no?
    – webmaniac
    Jan 24 '14 at 14:43
1

So here is what I used to increment the username's suffix by 1 if the initial value is not found:

function increment_string($str, $separator, $first = 1) {
    preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
    return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
}

$username = $firstname . ' ' . $lastname;
$result = db_query("SELECT uid FROM {users} WHERE name = :name", array(':name' => $username));
$results_number = $result->rowCount();
    if ($results_number == 0) {
        $form_state['values']['name'] = $username;  
    }
    else {
    while ($results_number >= 1) {
        $username = increment_string($username_to_store, ' ');
        $result = db_query('SELECT uid FROM {users} WHERE name LIKE :name_pattern', array(':name_pattern' => db_like($username)));
        $results_number = $result->rowCount();
    }
    $form_state['values']['name'] = $username;  
    }
0

Try below solution.

$firstname = $form_state['values']['field_firstname'][LANGUAGE_NONE][0]['value'];
$lastname = $form_state['values']['field_lastname'][LANGUAGE_NONE][0]['value'];
$username = $firstname.''.$lastname;
$result = db_query("SELECT uid FROM {users} WHERE name like '$username%'");
$results_number = $result->rowCount();
if($results_number == 0){
  $form_state['values']['name'] = $username;
}else{
  $form_state['values']['name'] = $username.$results_number;
}
2
  • This solution fails when at least one user gets deleted from the system. e.g. we have username_1 and username_2 and username_3 and username_2 is deleted. Then the above fails.
    – webmaniac
    Jan 24 '14 at 14:52
  • db_like() should be instead used.
    – apaderno
    Jan 24 '14 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.