title: Copy data to another table using sql query and model codeigniter
body: <p>i'm new to Codeigniter,i'm making something like item in cart system,using temporary table and main table.in my case now,i want to copy data from temporary table(cart) into my main table...but i'm using last insert id to get the ID of order number.
  here is my SQL tables<br></p>
  
  <pre><code>    Order            Order Detail                   Cart
    ID | DATE      ID | Order_ID | Item_ID         ID | Item_ID
                                                    1 |    1
                                                    2 |    2
  </code></pre>
  
  <p>and here is my save controller</p>
  
  <pre><code>function save()
   {
     extract($_POST);
     $date = date('Y-m-d')
     $data = array('date' =&gt; $date);
     $this-&gt;db-&gt;insert('order',$data);
     $order_id = $this-&gt;db-&gt;insert_id();
  
     //This is the copy process from Table Cart to Table Order Detail
     $row = $this-&gt;db-&gt;query('SELECT Item_ID from cart')-&gt;row();
  
     foreach($row as $key)
       {
         // I dont know what to loop....
       }
  
     $data1 = array('order_id' =&gt; $id, 'item_id'=&gt; $key-&gt;item_id);
     $this-&gt;db-&gt;insert('order_detail',$data1);
     $this-&gt;db-&gt;delete('cart');
   }
  </code></pre>
  
  <p>i already tried to put $data1 inside my loop,but it doesnt work...the data wont print out...and i tried using normal SQL Query</p>
  
  <pre><code>$this-&gt;db-&gt;query('INSERT INTO order_detail (item_id)
  SELECT item_id
  FROM cart');
  </code></pre>
  
  <p>but i need the variable $order_id inside the normal SQL query.
  is there any way to do it ? Thanks,..</p>