title: Generate unique ID from database PhpMyAdmin and CodeIgniter
body: <p>I have some problem with generating this unique ID.
  Here's the table looks like:
  <a href="http://i.stack.imgur.com/kUFSU.png" rel="nofollow">Table</a>
  <p>Here's the rule for creating the ID:</p>
  
  <ol>
  <li>The ID will looks like this: AAyyyymm00x<br/>
  
  <ul>
  <li>AA is the dest_code.</li>
  <li>yyyy is the current year.</li>
  <li>mm is the current month.</li>
  <li>00x is the registration number. And will be resetted each day to 1. And increased by 1 per-registration.</li>
  </ul></li>
  <li>So the final looks will be: AA201604001</li>
  </ol>
  
  <p>my code for controller:</p>
  
  <pre><code>public function tambah()
      {
          $this-&gt;data['namaNegara'] =  $this-&gt;registrasi_model-&gt;get_nama_negara();
          $this-&gt;data['main_view'] = 'program/administrasi/registrasi_form';
          $this-&gt;data['form_action'] = site_url('program/administrasi/registrasi/tambah');
  
          // Data untuk form.
          if (! $_POST) {
              $registrasi = (object) $this-&gt;registrasi_model-&gt;default_value;
          } else {
              $registrasi = $this-&gt;input-&gt;post(null, true);
          }   
  
          // Validasi.
          if (! $this-&gt;registrasi_model-&gt;validate('form_rules')) {
              $this-&gt;data['values'] = (object) $registrasi;
              $this-&gt;load-&gt;view($this-&gt;layout, $this-&gt;data);
              return;
          }
          $destName = $registrasi['Nama_Negara'];
          echo $registrasi['No_Registrasi'] = $this-&gt;registrasi_model-&gt;generate_no_reg($destName);
      }
  </code></pre>
  
  <p>my code for model:</p>
  
  <pre><code>public function generate_no_reg($destName)
      {
          $query = $this-&gt;db-&gt;query('SELECT Dest_Code from Destination WHERE Dest_Name= "$destName"');
          $id1 = $query-&gt;row()-&gt;Dest_Code;
  
          $query2 = 'SELECT Reg_ID FROM Registration ORDER BY Reg_ID DESC LIMIT 1';
          $lastid = $this-&gt;db-&gt;query($query2);
          $id2 = $lastid-&gt;row()-&gt;Reg_ID+1;
  
          $id3 = format_no_registrasi($id2);
          $kode = $id1.$id3;
          return $kode;
  
      }
  </code></pre>
  
  <p>my function code for format_no_registrasi:</p>
  
  <pre><code>function format_no_registrasi($no)
  { 
      $leadingzeros = '000';
      $no_reg = date('Y') . date('m') . substr($leadingzeros, 0, (-strlen($no))) . $no;
      return $no_reg;
  }
  </code></pre>
  
  <p>Here's my error: <a href="http://i.stack.imgur.com/v4AgT.png" rel="nofollow">error</a><br/> 
  The text that showing on the bottom is my ID i wanted but it dont have the Dest_Code.<br/>
  Any help is very much appreciated. And sorry for the trouble.</p>