0

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);
}
Rajan
  • 2,427
  • 10
  • 51
  • 111
  • You can do it by concatenating all your desired fields and then convert it into `base64_encode()` and store it to db – Disha V. Aug 08 '15 at 07:35
  • 1
    Such ["smart keys"](http://stackoverflow.com/a/28454136/3404097) are an antipattern. – philipxy Aug 08 '15 at 07:39
  • rather then voting down please let me know my mistakes as i am new to both codeigniter and Stackoverflow – Rajan Aug 08 '15 at 08:00
  • I expect that it was the wall of code. See [this](http://stackoverflow.com/help/mcve) and read other links [here](http://stackoverflow.com/help). (Reachable from the "help" on the SO banner.) – philipxy Aug 08 '15 at 08:28
  • I have updated the question please check – Rajan Aug 08 '15 at 10:39
  • i need to remove the down vote or else will not be able to chat and ask questions with masters. – Rajan Aug 08 '15 at 10:40
  • From a UX perspective.. Why would a concatenated string be better than just displaying this info? Have you heard of gravatars? The method of creating a unique default image for each user, makes it much easier for us humans to identify users. – EJTH Aug 08 '15 at 10:52

3 Answers3

1

why not,

 sha256( serialize( $user ));

Not saying it's necessarily a good idea to do this, but.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • want to create a key that helps admin to identify a user eg Jack-691-INDIA-01 so here Jack is the name 691 could be country code India and 01 would be his database id – Rajan Aug 08 '15 at 10:31
1

You can use any hash functions to generate a unique hash for a given string.

Some Popular ones :

You can use the hash() to try out various hash functions. Note than none of these are meant to be decrypted.

halfer
  • 19,824
  • 17
  • 99
  • 186
Arjun Komath
  • 2,802
  • 4
  • 16
  • 24
  • i want to create a key that helps admin to identify a user eg Jack-691-INDIA-01 so here Jack is the name 691 could be country code India and 01 would be his database id – Rajan Aug 08 '15 at 10:31
  • so you want to created a string that can be decoded back to its original string? – Arjun Komath Aug 08 '15 at 10:35
  • i dont want to encode a string i just want to concatenate the fields and make one string that can be saved in database which i call a key. like our phone is combination of country code area code the network provider code etc – Rajan Aug 08 '15 at 11:27
  • so all you have to do is append all the variables into one string : `$string = $var1.'-'.$var2.'-'.$var` – Arjun Komath Aug 08 '15 at 11:29
  • yes i know this but i am using MY_model by joost van and in that i have the get_new() method in my reseller_m where there is array of fields how to concatenate the fields in my controller – Rajan Aug 08 '15 at 12:14
  • You can pass those fields to the model. – Arjun Komath Aug 08 '15 at 12:17
  • so should i concatenate them in my model? – Rajan Aug 08 '15 at 12:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86538/discussion-between-rajan-and-arjun). – Rajan Aug 09 '15 at 06:23
  • i dont knw where to concenate and i even tried by i just got empty value in my key fielld – Rajan Aug 11 '15 at 05:40
1

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

}
Arjun Komath
  • 2,802
  • 4
  • 16
  • 24
  • can you show me how.. please i have updated the question please check. get_new function in model returns a new user i want to generate this key at time of user creation – Rajan Aug 11 '15 at 05:27
  • @Rajan I hope this explains the concept. – Arjun Komath Aug 11 '15 at 05:35
  • i didn't get you. i dont know where to concatenate the strings(fields). i have my save function in base model. In the model i have a function get_new which returns a new user – Rajan Aug 11 '15 at 05:39