【问题标题】:Store into a database table as row the query result将查询结果作为行存储到数据库表中
【发布时间】:2012-06-12 08:17:03
【问题描述】:

查询结果

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'] as $invent): 回声 $invent['ingredientID'];

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

我该怎么办? endforeach;

【问题讨论】:

  • $arr 定义在哪里?除了foreach,我在任何地方都看不到它。
  • 假设 $arr 是包含对象数组的数组
  • 我唯一能告诉你的是$arr['inventory'] 不是一个数组。试试var_dump

标签: php mysql arrays object


【解决方案1】:

假设:

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

你应该用它来打印它

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

【讨论】:

    猜你喜欢
    • 2021-11-09
    • 2012-11-05
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    相关资源
    最近更新 更多