如何在隐藏字段中发送购物车数据并在codeigniter控制器中获取它

时间:2013-07-02 07:20:11

标签: arrays codeigniter controller hidden

我正在使用codeigniter购物车库,我想从购物车获取数据并通过隐藏字段将其传递给控制器 以下是我的购物车数组

$cart = $this->cart->contents();

我在购物车中添加了三个项目,以下是var_dump()信息

array(3) { ["d3d9446802a44259755d38e6d163e820"]=> array(6) { ["rowid"]=> string(32) "d3d9446802a44259755d38e6d163e820" ["id"]=> string(2) "10" ["qty"]=> string(1) "1" ["price"]=> string(4) "1200" ["name"]=> string(11) "soup item 1" ["subtotal"]=> int(1200) } ["c51ce410c124a10e0db5e4b97fc2af39"]=> array(6) { ["rowid"]=> string(32) "c51ce410c124a10e0db5e4b97fc2af39" ["id"]=> string(2) "13" ["qty"]=> string(1) "1" ["price"]=> string(4) "1400" ["name"]=> string(11) "soup item 2" ["subtotal"]=> int(1400) } ["aab3238922bcc25a6f606eb525ffdc56"]=> array(6) { ["rowid"]=> string(32) "aab3238922bcc25a6f606eb525ffdc56" ["id"]=> string(2) "14" ["qty"]=> string(1) "1" ["price"]=> string(3) "800" ["name"]=> string(11) "soup item 3" ["subtotal"]=> int(800) } }

我从购物车中提取ID,名称,数量和价格并插入隐藏字段

<form action="http://localhost/food4u/site/order_now" method="post">
                            <input type="hidden" value="1" name="orderuserid">
            <input type="hidden" value="1" name="order_restid">
            <input type="hidden" value="238" name="orderDcharges">
            <input type="hidden" value="578" name="ordergst">
            <input type="hidden" value="3400" name="ordersubtotal">
            <input type="hidden" value="1" name="orderPmethod">

                                 <input type="hidden" value="10" name="cartitemid[]">
                 <input type="hidden" value="soup item 1" name="cartitemname[]">
                 <input type="hidden" value="1" name="cartitemqty[]">
                 <input type="hidden" value="1200" name="cartitemprice[]">
                                     <input type="hidden" value="13" name="cartitemid[]">
                 <input type="hidden" value="soup item 2" name="cartitemname[]">
                 <input type="hidden" value="1" name="cartitemqty[]">
                 <input type="hidden" value="1400" name="cartitemprice[]">
                                     <input type="hidden" value="14" name="cartitemid[]">
                 <input type="hidden" value="soup item 3" name="cartitemname[]">
                 <input type="hidden" value="1" name="cartitemqty[]">
                 <input type="hidden" value="800" name="cartitemprice[]">

- &GT;                  在我的控制器中,我得到这样的隐藏字段值

            $cartitemid = $this->input->post('cartitemid');
        $cartitemname = $this->input->post('cartitemname');
        $cartitemqty = $this->input->post('cartitemqty');
        $cartitemprice = $this->input->post('cartitemprice');

        print_r($cartitemid).'<br />';
        print_r($cartitemname).'<br />';
        print_r($cartitemqty).'<br />';
        print_r($cartitemprice).'<br />';
        exit;

它会给我结果

Array ( [0] => 10 [1] => 13 [2] => 14 ) Array ( [0] => soup item 1 [1] => soup item 2 [2] => soup item 3 ) Array ( [0] => 1 [1] => 1 [2] => 1 ) Array ( [0] => 1200 [1] => 1400 [2] => 800 ) 

我想在一个表中插入

$data = array(
                'Iem_Id' => ???,
                'Item_Name' => ???,
                'Item_Qty' => ???,
                'Item_Price' => ???,
                );

我想在一张表中插入以上所有数据,请指导我

1 个答案:

答案 0 :(得分:0)

你去吧

foreach($cartitemid as $key => $cartItem) {
    $data = array(
        'Iem_Id' => $cartItem,
        'Item_Name' => $cartitemname[$key],
        'Item_Qty' => $cartitemqty[$key],
        'Item_Price' => $cartitemprice[$key],
    );
    var_dump($data);
    echo "<br>";
    //$this->db->insert('table', $data);
    unset($data);
}

unset($cartItemId);
unset($key);