在我现有的用户名PHP(Codeigniter)中添加前缀

时间:2018-07-27 08:01:12

标签: php codeigniter-3

我正在尝试增强我以前的项目。到目前为止,在注册模块中,我已经完成了所有字段(字段是:名称,电子邮件,用户名和密码)的验证,检查电子邮件和用户名是否已经存在。

现在,我正在考虑通过为用户名添加前缀来为用户添加建议(如果用户名已存在)。

例如,在创建一个Google帐户时,如果已经使用了用户名,则会提示您。请参见下面的示例。

Example

我几乎明白了,但我认为我的代码有误或缺少某些内容。

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

在我的表格中,该用户名将类似于:xxxx1

它仍然是正确的,但是当用户现在尝试注册xxxx1时,它将变为xxxx11而不是xxxx2

已编辑:

问题:它现在可以正常工作,但它会像abcd一样循环,直到出现错误“最长执行时间超过30秒”,并且如何将其显示在我的js中?

注意:我在该论坛(How do I put extra number at the end of username if duplicate)中遵循了第一个答案,但这给了我很多循环。

我的控制器

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

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

        $this->Crud_model->insert('users',$insert_user);
        echo json_encode("success");
    }
}
while ($check_username); 

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

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


echo json_encode("success");

我的模型

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;

}

我的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();
  })
})

1 个答案:

答案 0 :(得分:0)

如果用户名已经存在,则提取用户名和add plus 1的最后一个数值。

$numVal = strrev( (int)strrev( $get_username ) );// returns last numeric value of username
 if($numVal ){
   $count = count($numVal );//counts number of digit
   $get_username = substr($get_username , 0, -($count));// subtract numeric valur from last of username
  }
 $numVal = $numVal+1;//increment 1 in numeric value
 $username = $get_username.$numVal;//concatenate 

使用以下脚本进行了测试:

$str= "xxxxx"; 
$newstr = strrev( (int)strrev( $str ) );  
if($newstr){
    $count = count($newstr);
  $str = substr($str, 0, -($count));
}
$newstr = $newstr+1;

echo $str.$newstr;

如果usename为xxxxx,则变为xxxxx1

如果用户名是xxxxx1则变成xxxxx2

如果用户名是xxxxx2,则变成xxxxx3,然后打开。