Add prefix in my existing username PHP(Codeigniter)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Add prefix in my existing username PHP(Codeigniter)



I am trying to enhance my previous project. In the part of registration module I already done so far the validation of all fields(fields are: name, email, username and password),check if the email and username is already existing.



Right now, I am thinking to add a suggestion for the user if the username is already existing by adding a prefix to the username.



For example in creating a google account, if the username is already taken it will suggest that. See the example below.



Example



I almost got it but I think my code is wrong or missing something.


Scenario 1: If user register existing username
//Assuming all fields are filled
Username: xxxx



In my table it will look like this username: xxxx1



It is still correct but when the user tried now to register xxxx1 it will become like this xxxx11 instead of xxxx2



Question: Why my $counter didnt work? How can I increment the prefix and display it to my js?



My Controller


$get_username = $_POST['username'];
$counter = 0;
$where = array(
"username" => $get_username
);

$check_username = $this->Crud_model->count_result('username','users',$where);
if($check_username > 0)
{
$counter++;
$username = $get_username . $counter;
$insert_user = array(
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'profile_picture' => "profile-picture.jpg",
'username' => $username,
'email' => $_POST['email'],
'password' => $_POST['password'],
'status' => 1,
);

$this->Crud_model->insert('users',$insert_user);


echo json_encode("success");

}
else
{

$insert_user = array(
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'profile_picture' => "profile-picture.jpg",
'username' => $username,
'email' => $_POST['email'],
'password' => $_POST['password'],
'status' => 1,
);

$this->Crud_model->insert('users',$insert_user);


echo json_encode("success");
}



My model


public function count_result($tag,$table,$where)
{
$this->db->select($tag);
$this->db->from($table);
$this->db->where($where);
$num_results = $this->db->count_all_results();
return $num_results;

}



My JS


$(document).ready(function(){
$("#registration-form").on('submit',function(e){
$.ajax({
url: base_url+"formsubmit/new_form_submit",
type: "POST",
data: $(this).serialize(),
success:function(data)
{
var result = JSON.parse(data);

if(result === "success")
{
$("h5").html("");
success_message("#success-message-new-account","Create Successful!");
window.setTimeout(function(){location.href=base_url},2000);
}
else{
$("#first_name_error").html(result.first_name_error);
$("#last_name_error").html(result.last_name_error);
$("#username_error").html(result.username_error);
$("#email_error").html(result.email_error);
$("#password_error").html(result.password_error);
}
},
error: function(data) {
alert('error');
}
})
e.preventDefault();
})
})





Do you want to insert unique username. Suppose username is xxxxx and user exists then suggestion will like xxxxx1' if user xxxxx1` also exists then suggestion will like xxxxx11 am I right?
– Gufran Hasan
25 mins ago


xxxxx


xxxxx1' if user


xxxxx11





Yes,I want to have a unique username and if user xxxxx1 also exists then it will suggest xxxxx2 not xxxxx11
– Angel
21 mins ago





@Angle, you should pass counter from ajax call
– Gufran Hasan
13 mins ago




1 Answer
1



If the username is already existed then extract the last numeric value of username and add plus 1.


add plus 1


$numVal = strrev( (int)strrev( $get_username ) );
$numVal = $numVal+1;
$username = $get_username.$numVal;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Better method to check all objects' parameters with a single call

Visual Studio Code: How to configure includePath for better IntelliSense results