title: Auto increment data on submit in CodeIgniter
body: <p>I'm building a small CodeIgniter CRUD application, a very basic one to be precise. Now, in my application, I need to auto increment <code>roll</code> data whenever I insert a new student in the following way:</p>
  
  <pre><code>    20141001
      20141002
        ...
        ...
      20141099
      20141100
      20142001
      20142002
        ... 
        ...
        ...
      20145099
      20145100
      20151001
      20151002
        ...
        ...
      20151100
      20152001
        ...
        ...
  </code></pre>
  
  <p>And so on.
  My controller - <code>student.php</code>:</p>
  
  <pre><code>    &lt;?php
      if ( ! defined('BASEPATH')){ exit('No direct script access allowed');}
      class Student extends CI_Controller
      {
          function __construct()
          {
              parent::__construct();
              #$this-&gt;load-&gt;helper('url');
              $this-&gt;load-&gt;model('student_model');
          }
  
          //Show all Students
          public function index()
          {
              $data['student_list'] = $this-&gt;student_model-&gt;get_all_students();
             $this-&gt;load-&gt;view('student_view', $data);
          }
  
  
         //Insert a student
         public function insert_student_db()
         { 
              $sdata['name'] = $this-&gt;input-&gt;post('name');
              //Auto-increment, auto-generate roll, for each entry
              $sdata['roll'] = '20141000';
              for($i = 0; $i &lt; 8; $i++) 
              {
                  $sdata['roll']++;
                  if(substr($sdata['roll'], 5) == '100')
                  {
                      continue;
                  }
              } 
              $sdata['department'] = $this-&gt;input-&gt;post('department');
              $sdata['email'] = $this-&gt;input-&gt;post('email');
              $sdata['mobile'] = $this-&gt;input-&gt;post('mobile');
              $res = $this-&gt;student_model-&gt;insert_student($sdata);
              if($res)
              {
                  header('location:'.base_url()."index.php/student/".$this-&gt;index());
  
              }
  
          }
  
      }
      ?&gt;
  </code></pre>
  
  <p>My model - <code>student_model.php</code>:</p>
  
  <pre><code>&lt;?php
  class Student_model extends CI_Model 
  {
      function __construct()
      {
          parent::__construct();
          $this-&gt;load-&gt;database();
      }   
  
      //To retrieve all students
      public function get_all_students()
      {
          $query = $this-&gt;db-&gt;get('student');
          return $query-&gt;result();
      }
  
      //To add a new student to the database
      public function insert_student($data)
      {
          return $this-&gt;db-&gt;insert('student', $data);
      }
  
  }
  ?&gt;
  </code></pre>
  
  <p>And it gives me weird results - instead of incrementing, all my entries are assigned one single roll, which is <code>20141008</code></p>
  
  <p>Screenshot of the problematic output I'm getting: 
  <img src="https://i.stack.imgur.com/GL0zg.jpg" alt="Screenshot"> </p>
  
  <p>My table is <code>student</code>, which has the following structure:</p>
  
  <pre><code>name             varchar(30)
  roll             varchar(10)
  department       varchar(10)
  email            varchar(50)
  mobile           varchar(15)
  </code></pre>
  
  <p>Please note that <code>roll</code> is not a Primary Key column, and it's not unique.</p>
  
  <p>How can I solve my problem in context of CodeIgniter?</p>