doukao2180 2015-08-08 07:28
浏览 32
已采纳

如何通过在PHP CodeIgniter中连接用户信息来生成唯一键?

I want to create keys based on the information provided by the user. For example, if a user inserts their name, phone, country and some other information then my code should concatenate some fields and store a unique key into the same user table where I have field key. Example:

JACK-691-INDIA-10-001

So here, Jack would be the name inserted by user, 691 would be country code India is name of country, 10 is no of users and 001 is his database id.

In model I have a function get_new() where I initialize all the variables in array and I want to concatenate some variables and form a string(key) to be saved in key field.

The Model:

public function get_new(){
    $user = new stdClass();

//          $user->id = '';
    $user->sip_username='';
    $user->sip_password='';
    $user->key='';
    $user->allocation_block='';
    $user->name='';
    $user->email = '';      
    $user->password = '';
    $user->phone=''; 
    $user->user_num=''; 
    $user->address = '';
    $user->status = '';
    $user->country=''; 
    $user->created = '';
    $user->modified  = '';
    $user->balance = '';
    return $user;
    $this->session->set_userdata($data);

}

The Controller Edit method :

    public function edit ($id = NULL)
{
    // Fetch a user or set a new one
    if ($id) {
        $this->data['user'] = $this->reseller_m->get($id);
        count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
    }
    else {
        $this->data['user'] = $this->reseller_m->get_new();
    }

    // Set up the form
    $rules = $this->reseller_m->rules_admin;
    $id || $rules['password']['rules'] .= '|required';
    $this->form_validation->set_rules($rules);

    // Process the form
    if ($this->form_validation->run() == TRUE) {

$data = $this->reseller_m->array_from_post(array('sip_username','sip_password','key','allocation_block','name','email','password','phone','balance','user_num','address','country','created','modified','status'));

        $data['password'] = $this->reseller_m->hash($data['password']);


        $key=$this->reseller_m->save($data, $id);

         $this->db->insert_id();

/* $key=$this->reseller_m->save($data, $key); $data['key'] =$this->reseller_m->insert_item($data['name'].$data['phone']);

*/

        for($i=1; $i<=$data['user_num'];$i++)
            {
            $userdata=array('key'=>$key);
        // here users is taken name of user table with retailer_id is field
            $this->user_m->save($userdata,$id);
             }



        redirect('admin/reseller');
    }

    // Load the view
    $this->data['subview'] = 'admin/reseller/edit';
    $this->load->view('admin/_layout_main', $this->data);
}
  • 写回答

3条回答 默认 最新

  • dongnai3960 2015-08-08 12:39
    关注

    Yes, you can pass the required values to the model, concatenate and create the string there, store it to database.

    From the controller you can generate the key and then pass it to model :

    $key = 'JACK-691-INDIA-10-001' // you have to concatenate the proper string, what is shown here is just as an example.
    $this->load->model('users'); //the model in which the get_new function exsists
    $this->users->get_new($key);
    

    Model :

    public function get_new($key) {
        $user = new stdClass();
    
    //          $user->id = '';
        $user->sip_username='';
        $user->sip_password='';
        $user->key='';
        $user->allocation_block='';
        $user->name='';
        $user->email = '';      
        $user->password = '';
        $user->phone=''; 
        $user->user_num=''; 
        $user->address = '';
        $user->status = '';
        $user->country=''; 
        $user->created = '';
        $user->modified  = '';
        $user->balance = '';
        return $user;
        $this->session->set_userdata($data);
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义