Search code examples
phparrayscodeignitercontrollerhidden

How to send cart data in hidden field and fetch it in codeigniter controller


I am using codeigniter cart library and I want to get data from cart and pass it to controller via hidden field following is my cart array

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

i have added three item in cart and following is var_dump() info

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

i have extract id, name, qty and price from cart and insert into hidden field,

<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[]">

--> and in my controller i get these hidden field value like this

            $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;

and it will give me result

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 ) 

and i want to insert this in only one table

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

i want to insert all above data in one table please guide me


Solution

  • Here you go

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