作为查询结果行存储到数据库表中

时间:2012-06-08 09:10:51

标签: php mysql arrays object

查询结果

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

我想在数组['inventory']中显示所有对象数组。

我做到了这一点     foreach($ arr ['inventory']为$ invent):             echo $ invent ['ingredientID'];

但是foreach部分有一个错误。 错误:为foreach()提供的参数无效

我该怎么办?         endforeach;

1 个答案:

答案 0 :(得分:1)

假设:

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

你应该用它来打印它

foreach($results['inventory'] as $inventory)
{
    print_r($inventory->ingredientID);
}
相关问题