繁体   English   中英

多维数组到Mysql Codeigniter

[英]Multidimensional Array To Mysql Codeigniter

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

sales_table

 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
        )
)

提出一种使用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);
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM