title: Codeigniter: How to make Autonumber
body: <p>I'm developing application that need unique code such as:
  TS001, TS0002, TS003 </p>
  
  <p>I have PHP source code for autonumber like this:</p>
  
  <pre><code>function autoNumber($tabel, $kolom, $lebar=0, $awalan) {
      $q = mysql_query("SELECT $kolom FROM  $tabel ORDER BY $kolom DESC LIMIT 1");
      $jum = mysql_num_rows($q);
  
  
      if($jum == 0) {
          $nomor = 1;
      } else {
          $data = mysql_fetch_array($q);
          $nomor = intval(substr($data[0],strlen($awalan))) + 1;
      }
  
      if($lebar &gt; 0) {
  
          $angka = $awalan.str_pad($nomor,$lebar,"0",STR_PAD_LEFT);
  
      } else {
  
          $angka = $awalan.$nomor;
      }
      return $angka;
  
  }
  </code></pre>
  
  <p>But i have a problem when i try it on Codeigniter.</p>
  
  <p>My latest code on Codeigniter :</p>
  
  <pre><code>function autoNumber($tabel, $kolom, $lebar=0, $awalan) {
      $q = $this-&gt;db-&gt;query("SELECT $kolom FROM  $tabel ORDER BY $kolom DESC LIMIT 1");
  
  
      $this-&gt;db-&gt;order_by($kolom, "desc");
      $this-&gt;db-&gt;limit(1);
      $this-&gt;db-&gt;from('my_table');
      $data['jum'] = $this-&gt;db-&gt;count_all_results();
  
  
      foreach ($jum as $r) {
          # code...
  
  
      if($jum == 0) {
          $nomor = 1;
      } else {
          $data = mysql_fetch_array($q);
          $nomor = intval(substr($data[0],strlen($awalan))) + 1;
      }
  
      if($lebar &gt; 0) {
  
          $angka = $awalan.str_pad($nomor,$lebar,"0",STR_PAD_LEFT);
  
      } else {
  
          $angka = $awalan.$nomor;
      }
  
  
  
      }
  
       return $angka;
  
  
  }
  </code></pre>