title: Multiple inserts in a loop codeigniter
body: <p>Let's say in my DB i have 2 tables. One is named <strong>Father</strong> with <strong>Father_id</strong> as <strong>PK</strong> and <strong>Father_name</strong> as a row. Then i have the second table as <strong>Children</strong> with <strong>Child_id</strong> as <strong>PK</strong>, <strong>Father_id</strong> as <strong>FK</strong>, and <strong>Child_name</strong> as a row.</p>
  
  <p>Children would have a relationship with Father as Many to One. Many Children to One Father.</p>
  
  <p>So let's say the data looks like this in my controller.</p>
  
  <pre><code>Array([Father_id] =&gt; 1 [Children] =&gt; Array([0] =&gt; John [1] =&gt; Peter [2] =&gt; Michael))
  </code></pre>
  
  <p>I pass the data above to my model to insert right but i have to restructure it in a way that i can just send it to my database to insert.</p>
  
  <pre><code>foreach($data as $row){
      for($temp = 0; $temp &lt; count($data['Child_name']); $temp++){
          $query[$temp] = array('Father_id' =&gt; $data['Father_id'], 'child_name' =&gt; $row[$temp]);
      }
  }
  </code></pre>
  
  <p>So now it looks like this,</p>
  
  <pre><code>Array([0]=&gt;Array([Father_id] =&gt; 1 [Child_name] =&gt; John)[1]=&gt;Array([Father_id] =&gt; 1 [Child_name] =&gt; Peter)[2]=&gt;Array([Father_id] =&gt; 1 [Child_name] =&gt; Michael))
  </code></pre>
  
  <p>I understand how to insert this, i'll just have to do another foreach loop or i can just remove the <code>$temp</code> in <code>$query[$temp]</code> and instead put an insert query inside that loop.</p>
  
  <p>My problem is that how can i know that each insert is successful, other than checking the database, i would like to have a return statement like if its only a single insert then if its successful it returns 1.</p>
  
  <p>Also is there any better way to do multiple inserts?</p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>After some needed break i came back and made some changes.</p>
  
  <p>This is now my model function:</p>
  
  <pre><code>        public function insert_children($data){
          foreach($data['child_name'] as $row){
              $query = array('father_id' =&gt; $data['father_id'], 'child_name' =&gt; $row);
              $result = $this-&gt;db-&gt;insert('children',$query);
          }
    //return something here
  }
  </code></pre>
  
  <p>It is essential to return something back to the controller right? for example, i want to return if the inserts were successful or not. So is there any already made function in codeigniter for this? or do i have to make myself.</p>
  
  <p>for example, i can just make a <code>$temp</code> variable to contain an incremental value each time an insert is successful and compare it at the end of the loop to the <code>count($data['children'])</code></p>
  
  <p>So if there are 3 children the value of $temp should be 3 or 2 depending on declaration if i specify it with 1 like <code>$temp = 1;</code></p>