title: How to genrate a unique key for user from database fields using php codeigniter?
body: <p>I want to create keys based on the information provided by the User. For eg If user insert his name,phone,country,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. I hope you understand my issue.I am new to codeigniter and have used the base model by Joost Van.</p>
  
  <p>The Controller Code:</p>
  
  <pre><code>&lt;?php
  class Reseller extends Admin_Controller
  {
  
  public function __construct ()
  {
      parent::__construct();
  }
  
  public function index ()
  {
      // Fetch all users
      $this-&gt;data['users'] = $this-&gt;reseller_m-&gt;get();
  
      // Load view
      $this-&gt;data['subview'] = 'admin/reseller/index';
      $this-&gt;load-&gt;view('admin/_layout_main', $this-&gt;data);
  }
      public function reseller ()
  {
      // Fetch all users
      $this-&gt;data['users'] = $this-&gt;reseller_m-&gt;get();
  
      // Load view
      $this-&gt;data['subview'] = 'admin/reseller/users'; //Users variable stores the suvbview 
      $this-&gt;load-&gt;view('admin/_layout_main', $this-&gt;data);
  }
  
  
  public function edit ($id = NULL)
  {
      // Fetch a user or set a new one
      if ($id) {
          $this-&gt;data['user'] = $this-&gt;reseller_m-&gt;get($id);
          count($this-&gt;data['user']) || $this-&gt;data['errors'][] = 'User could not be found';
      }
      else {
          $this-&gt;data['user'] = $this-&gt;reseller_m-&gt;get_new();
      }
  
      // Set up the form
      $rules = $this-&gt;reseller_m-&gt;rules_admin;
      $id || $rules['password']['rules'] .= '|required';
      $this-&gt;form_validation-&gt;set_rules($rules);
  
      // Process the form
      if ($this-&gt;form_validation-&gt;run() == TRUE) {
  
  
  
  $data = $this-&gt;reseller_m-&gt;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-&gt;reseller_m-&gt;hash($data['password']);
  
  
          $key=$this-&gt;reseller_m-&gt;save($data, $id);
          for($i=1; $i&lt;=$data['user_num'];$i++)
              {
              $userdata=array('key'=&gt;$key);
          // here users is taken name of user table with retailer_id is field
              $this-&gt;db-&gt;insert('users',$userdata);
               }
  
  
  
          redirect('admin/reseller');
      }
  
      // Load the view
      $this-&gt;data['subview'] = 'admin/reseller/edit';
      $this-&gt;load-&gt;view('admin/_layout_main', $this-&gt;data);
  }
  
  
  public function delete ($id)
  {
      $this-&gt;reseller_m-&gt;delete($id);
      redirect('admin/reseller');
  }
  
  
  }   
  </code></pre>
  
  <p>The Model:</p>
  
  <pre><code>&lt;?php
  class Reseller_M extends MY_Model {
  
  
  protected $_table_name = 'reseller';
  protected $_order_by = 'name';
  protected $_timestamps = TRUE;
  public $rules = array(
      'email' =&gt; array(
          'field' =&gt; 'email', 
          'label' =&gt; 'Email', 
          'rules' =&gt; 'trim|required|valid_email|xss_clean'
      ), 
      'password' =&gt; array(
          'field' =&gt; 'password', 
          'label' =&gt; 'Password', 
          'rules' =&gt; 'trim|required'
      ),
  
  );
  public $rules_admin = array(
      'name' =&gt; array(
          'field' =&gt; 'name', 
          'label' =&gt; 'Name', 
          'rules' =&gt; 'trim|required|xss_clean'
      ), 
      'email' =&gt; array(
          'field' =&gt; 'email', 
          'label' =&gt; 'Email', 
          'rules' =&gt; 'trim|required|valid_email|callback__unique_email|xss_clean'
      ), 
      'password' =&gt; array(
          'field' =&gt; 'password', 
          'label' =&gt; 'Password', 
          'rules' =&gt; 'trim|matches[password_confirm]'
      ),
      'password_confirm' =&gt; array(
          'field' =&gt; 'password_confirm', 
          'label' =&gt; 'Confirm password', 
          'rules' =&gt; 'trim|matches[password]'
      ),
      'sip_username' =&gt; array(
          'field' =&gt; 'sip_username', 
          'label' =&gt; 'Sip Username', 
          'rules' =&gt; 'trim|required|'
      ),
      'sip_password' =&gt; array(
          'field' =&gt; 'sip_password', 
          'label' =&gt; 'SIP Password', 
          'rules' =&gt; 'trim|required|'
      ),
  
  
  );
  
  function __construct ()
  {
      parent::__construct();
  
  }
  
  
  
  
  public function get_new(){
      $user = new stdClass();
  
  //          $user-&gt;id = '';
      $user-&gt;sip_username='';
      $user-&gt;sip_password='';
      $user-&gt;key='';
      $user-&gt;allocation_block='';
      $user-&gt;name='';
      $user-&gt;email = '';      
      $user-&gt;password = '';
      $user-&gt;phone=''; 
      $user-&gt;user_num=''; 
      $user-&gt;address = '';
      $user-&gt;status = '';
      $user-&gt;country=''; 
      $user-&gt;created = '';
      $user-&gt;modified  = '';
      $user-&gt;balance = '';
      return $user;
      $this-&gt;session-&gt;set_userdata($data);
  
  }
  
  public function get_user_profile(){
      $this-&gt;db-&gt;select('id,sip_password,sip_username,phone,key,address,country,created,modified,user_num,allocation_block,status,email, name, balance');
      $query = $this-&gt;db-&gt;get_where('reseller', array('id' =&gt; $this-&gt;session-&gt;userdata('id')));
      return $query-&gt;result();
  }
  
  function insert_item($key)
  
  {
      $this-&gt;reseller = $key;
  
      $this-&gt;db-&gt;set($this-&gt;reseller, $this);
  }
  
  
  
  
  public function hash ($string)
  {
      return hash('sha512', $string . config_item('encryption_key'));
  }
  
  
  public function loggedin ()
  {
      return (bool) $this-&gt;session-&gt;userdata('loggedin');
  }
  
  }
  </code></pre>
  
  <p>The Base Model Where CURD Functions are defined </p>
  
  <pre><code>&lt;?php
  class MY_Model extends CI_Model {
  
  protected $_table_name = '';
  protected $_primary_key = 'id';
  protected $_primary_filter = 'intval';
  protected $_order_by = '';
  public $rules = array();
  protected $_timestamps = FALSE;
  
  function __construct() {
      parent::__construct();
  }
  
  public function array_from_post($fields){
      $data = array();
      foreach ($fields as $field) {
          $data[$field] = $this-&gt;input-&gt;post($field);
      }
      return $data;
  }
  
  
  public function get($id = NULL, $single = FALSE){
  
      if ($id != NULL) {
          $filter = $this-&gt;_primary_filter;
          $id = $filter($id);
          $this-&gt;db-&gt;where($this-&gt;_primary_key, $id);
          $method = 'row';
      }
      elseif($single == TRUE) {
          $method = 'row';
      }
      else {
          $method = 'result';
      }
  
  
      return $this-&gt;db-&gt;get($this-&gt;_table_name)-&gt;$method();
  }
  
  public function get_by($where, $single = FALSE){
      $this-&gt;db-&gt;where($where);
      return $this-&gt;get(NULL, $single);
  }
  
  public function save($data, $id = NULL){
  
  
  
  
      // Set timestamps
      if ($this-&gt;_timestamps == TRUE) {
          $now = date('Y-m-d H:i:s');
          $id || $data['created'] = $now;
          $data['modified'] = $now;
      }
  
      // Insert
      if ($id === NULL) {
          !isset($data[$this-&gt;_primary_key]) || $data[$this-&gt;_primary_key] = NULL;
          $this-&gt;db-&gt;set($data);
          $this-&gt;db-&gt;insert($this-&gt;_table_name);
          $id = $this-&gt;db-&gt;insert_id();
      }
      // Update
      else {
          $filter = $this-&gt;_primary_filter;
          $id = $filter($id);
          $this-&gt;db-&gt;set($data);
          $this-&gt;db-&gt;where($this-&gt;_primary_key, $id);
          $this-&gt;db-&gt;update($this-&gt;_table_name);
      }
  
      return $id;
  }
  
  public function delete($id){
      $filter = $this-&gt;_primary_filter;
      $id = $filter($id);
  
      if (!$id) {
          return FALSE;
      }
      $this-&gt;db-&gt;where($this-&gt;_primary_key, $id);
      $this-&gt;db-&gt;limit(1);
      $this-&gt;db-&gt;delete($this-&gt;_table_name);
  }
  }
  </code></pre>