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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行