0

一点帮助如何使用此数组创建输入查询?或者我怎样才能在表格中传递它?我的表格布局示例是这样的:

销售表

 id   |   product_name   |   price   |   qty   |   subtotal       

这是从 var_dump() 函数派生的。
这是我在数组中的代码

//array code from unserialized function
    $products = unserialize($this->session->userdata('product_list'));

//This is the output.
Array
(
    [2] => Array
        (
            [id] => 2
            [product_name] => NOKIA 5110
            [product_desc] => Cellphone
            [product_price] => 500.00
            [product_qty] => 1
            [product_amount] => 500
            [product_code] => NOKI2012-84353
        )

    [3] => Array
        (
            [id] => 3
            [product_name] => HP IPAQ RW6828
            [product_desc] => Cellphone
            [product_price] => 1500.00
            [product_qty] => 1
            [product_amount] => 1500
            [product_code] => HP I2012-08386
        )
)
4

1 回答 1

0

使用 foreach 循环创建一种插入方法,该循环将迭代所有产品并逐个插入它们

foreach($products as $p) {
    $data = array(
        'product_name' => $p['product_name'],
        'price' => $p['product_price'],
        'qty' => $p['product_qty'],
        'subtotal' => 1, // what do you want here?
    );

    $data2 = array(
        'product_stuff' => $p['product_code'],
        'product_stuffo' => 100 * $p['product_amount'], // anything you want
    );


    $this->db->insert('products', $data);
    $this->db->insert('sales_line', $data2);
}

您可能希望稍后让此代码支持事务。

于 2012-09-25T09:37:59.403 回答