title: codeigniter get last insert id from model to use as redirect parameter
body: <p>I need to get the insert ID to be used in my controller, the problem is my model function has several data arrays, and I need the insert ID of the 1st array. therefor if I try to get the insert ID on my controller obviously its grabbing the 2nd insert ID and not the 1st.</p>
  <p>I'm not sure this makes sense but here is an example:</p>
  <h1>Model</h1>
  <pre><code>public function add() { 
  
      $data = array(
          'name' =&gt; $_POST['companyName']
          ,'type' =&gt; $_POST['companyType']
          ,'active' =&gt; '1');
  
      $this-&gt;db-&gt;insert('company', $data);
  
      $cid = $this-&gt;db-&gt;insert_id();
  
      $data2 = array(
          'cid' =&gt; $cid
          ,'name' =&gt; $_POST['locationName']
          ,'address' =&gt; $_POST['locationAddress']);
  
      $this-&gt;db-&gt;insert('locations', $data2);
  }
  </code></pre>
  <p>This is working fine...However after this runs, on my controller the redirect is getting the data2 last insert which makes my redirect wrong.</p>
  <h1>Controller</h1>
  <pre><code>public function add() {
  
      if (isset($_POST[&quot;add&quot;]))
      {
          $this-&gt;Company_model-&gt;add();
          $id = $this-&gt;db-&gt;insert_id();
          redirect('company/view/'.$cid);
      }
  
      $data['states']  = $this-&gt;Company_model-&gt;
  
  }
  </code></pre>
  <p>Any help would be greatful!</p>