如果CodeIgniter中已存在数值,如何将其添加到值

时间:2018-06-30 10:18:13

标签: php mysqli codeigniter-3

因此在我的注册表中,我有一个唯一的输入,但是如果有两个以上的用户想要创建一个准确的用户名,我希望创建的第二个用户名在键入的值之后有一个数字。

例如,我们有两个用户:

用户名1:编解码器

用户名2:codelover2,依此类推。

我当前在我的控制器中有这个

    // Create Page Data Array
        $data = array(
            'first_name' => strip_tags($this->input->post('first_name')),
            'last_name'  => strip_tags($this->input->post('last_name')),
            'email'      => strip_tags($this->input->post('email')),
            //'username'   => strip_tags($this->input->post('username')),
            'ip_address' => $this->input->ip_address(),
            'genre'      => $this->input->post('genre'),
            'slug'       => $slug,
            'status'    =>  'active',
            /* 1-Founder, 2-Administrator, 3-Editor, 4-Author , 5-Contributor , 6-Subscriber , 7-Banned, 8-Suspended */
            'role'       => 'Subscriber',
            'password'   => strip_tags(password_hash($this->input->post('password'), PASSWORD_DEFAULT)),
            'password2'  => strip_tags(password_hash($this->input->post('password2'), PASSWORD_DEFAULT)),
        );
// Is this the right approach of doing this?
            $i = 0;
            while($this->input->post('username') != 0){
$i++;
                $data['username'] = strip_tags($this->input->post('username'.'.'.$i));
            }
            //Check if username and/or email already exist.
            $existent_username = $this->User_model->existent_username($this->input->post('username'));
            $existent_email = $this->User_model->existent_email($this->input->post('email'));

            if (!empty($existent_username) || ($existent_email)) {
                // Create Message
                $this->session->set_flashdata('error', 'Username and/or Email already exist. Try again.');              

                // Redirect to pages
                redirect('users/register');
            } else {
                // Add User
                $this->User_model->add($data);

在我的模型中检查用户名是否已经存在:

public function existent_username($username) {
    $query = $this->db->get_where($this->table, array('username' => $username));
    return $query->row_array();
}

这是我要在CodeIgniter中执行的操作:

if(empty($existent_username)){
                    $query = mysqli_query($con, "SELECT username FROM users WHERE username = '$username'");

                    $i = 0;
                    // If username already exists, add number to username
                    while(mysqli_num_rows($query) != 0){
                        $i++;
                        $username = $username ."." . $i;
                        $query = mysqli_query($con, "SELECT username FROM users WHERE username = '$username'");
                    }
                }

就这样了,谢谢。

1 个答案:

答案 0 :(得分:1)

首先

// Is this the right approach of doing this?
$i = 0;
while($this->input->post('username') != 0) {
    $i++;
    $data['username'] = strip_tags($this->input->post('username'.'.'.$i));
}

这是错误的,在我看来,您正在此处运行无限循环。我什至不明白你为什么需要它。如果用户可以提供多个用户名,则首先获取输入字段的值,然后运行循环。

现在关于主要问题, 您应该获取所有与提供的用户名部分匹配的现有用户名。然后生成用户名并检查用户名是否已经存在。如果不是,那就是用户名。这样。

$username = $this->input->post('username');
$existent_username = $this->User_model->existent_username($username));
if (!empty($existent_username)) {
    $partially_matched_usernames = $this->db->select('username')->like('username', $username, 'after')->from('users')->get()->result_array();
    $usernames_array = array_column($partially_matched_usernames, 'username');

    // now generate the username
    $i = 1;
    while(true) {
        if( ! in_array($username.$i, $usernames_array)) {
            $username = $username.$i;
            break;
        }
        $i++;
    }
    echo $username;
}

我没有运行此代码,因此可能有任何错误。我认为您将可以自己解决该问题。很高兴编写代码...

相关问题