2012-06-08 56 views
0

查詢結果存儲到數據庫表中的行查詢結果

Array 
(
[0] => stdClass Object 
    (
     [ingredientID] => 2 
     [code] => Bf 
     [description] => 1st Class Flour 
     [volume] => 8268 
     [price] => 750 
     [amount_gram] => 0.02980 
     [status] => Inactive 
     [uom_id] => 1 
     [flour] => Yes 
    ) 

[1] => stdClass Object 
    (
     [ingredientID] => 3 
     [code] => Sf 
     [description] => 3rd Class Flour 
     [volume] => 18490 
     [price] => 635 
     [amount_gram] => 0.02540 
     [status] => Inactive 
     [uom_id] => 5 
     [flour] => Yes 
    ) 
.......... 

我想保存這導致到另一個表作爲行庫存。 表看起來就像這樣:

ID  inventory 
1   (the result) 
2   (another result) 

後,我會回來再次查詢,以便我可以顯示結果。

這是我最近做的事情。

商店:

//getting the result 
$inv = $this->db->get_from('table','id'=>'1')->row(); 
<input type="hidden" name="inventory" value="<?php print_r($inv)?>"> 
//storing in the new table 
$this->db->insert('table2',array('inventory'=>$this->input->post('inventory'))); 

得到:

$inventory = $this->db->get_where('table2',array('ID'=>'1'))->row_array(); 
//result 
array 
(
     [ID] => 1 
     [inventory] => 
     array 
     (
      [0] => stdClass Object 
      (
       [ingredientID] => 2 
       ...... and so on 

我想顯示陣列[ '庫存'〕中的一切是對象的數組。

我已經完成了這個 foreach($ arr ['inventory'] as $ invent): echo $ invent ['ingredientID'];

但在foreach部分有錯誤。 錯誤:爲foreach提供的參數無效()

我該怎麼辦? endforeach;

+0

哪裏是'$ arr'定義?除了'foreach'我什麼都看不到。 – Leri

+0

假設$ arr是包含對象數組的數組 – Jetoox

+0

我唯一能告訴你的是'$ arr ['inventory']'不是數組。試試'var_dump'吧 – Leri

回答

1

假設:

$results = $this->db->get_where('table2',array('ID'=>'1'))->row_array(); 

,你應該用它來打印

foreach($results['inventory'] as $inventory) 
{ 
    print_r($inventory->ingredientID); 
} 
相關問題