将数据插入表中

时间:2016-07-25 14:47:55

标签: php codeigniter mysqli cart

我把die放在我的代码中,看看输出是正确的,输出在这里。 http://prntscr.com/bxam1f然后问题是我将它插入表中。它插入到表中但只有1个名称,这是要插入的最后一行。这是http://prnt.sc/bxanpe

我的代码在这里

if ($cart = $this->cart->contents()):
foreach ($cart as $item):
    $transac_id  = rand(10000,99999);

    $ship_region =  $this->input->post('ship_region');
    $get_region = $this->PaymentModel->get_ship_region($ship_region);
    foreach($get_region as $add)    
    {

        $addboxs = $add->box_small;
        $addboxm = $add->box_med;
        $addboxl = $add->box_large;
        $addpouchs = $add->pouch_small;
        $addpouchl = $add->pouch_large;

    }
    //foreach
    echo "<br><br>";
        echo "CART CONTENTS <br>";
        $lbc_status = 2; // 1 = box, 2 = no box
        $cart_size = 0;
        $qty = 0; // Start 0
        $subtotal_price = 0;
        $prodweight = 0;
        $pouch_size = 0;
        $cart_array = array();

        foreach($cart = $this->cart->contents() as $item)
        {
            $cart_size += 1;
            $qty += $item['qty'];
            array_push($cart_array,$item);
            $prodweight += $item['prod_weight'] * $item['qty'];
            $subtotal_price += $item['subtotal'];
            echo "ITEM ID: " . $item['id'] . "<br>";
            echo "ITEM QTY: " . $item['qty'] . "<br>";
            echo "ITEM PRICE: " . $item['price'] . "<br>";
            echo "ITEM NAME: " . $item['name'] . "<br>";
            echo "LBC TYPE: " . $item['lbc_type'] . "<br>";
            echo "ITEM WEIGHT IN GRAMS: ". $item['prod_weight'] . "<br>";
            echo "POUCH SIZE: " . $item['pouch_size'] . "<br><br>";

            if($item['lbc_type'] == "Box") {
                $lbc_status = 1;
            } //box
        }              
        if($ship_region == 'Luzon')
        {

            $kg_product = 0.001 * $prodweight;
            $roundoff = round($kg_product,0);
                if($lbc_status == 1) {
                    if($roundoff >=0 && $roundoff <=3)
                    {
                        echo "TOTAL ITEMS PURCHASED (QTY): " . $qty . "<br>";
                        echo "SUBTOTAL (SUBTOTAL): Php. " . $subtotal_price . "<br>";
                        echo "Shipping Fee: ". $addboxs . "<br>";
                        echo "PRODUCT WEIGHT IN KILO: " . $roundoff . "<br>";
                        $total = $subtotal_price + $addboxs;
                        $sizes = $addboxs;
                        echo "GRAND TOTAL: Php. " . $total . "<br><br>";
                    }
                    elseif($roundoff >=4 && $roundoff <=6)
                    {
                        echo "TOTAL ITEMS PURCHASED (QTY): " . $qty . "<br>";
                        echo "SUBTOTAL (SUBTOTAL): Php. " . $subtotal_price . "<br>";
                        echo "Shipping Fee: ". $addboxm . "<br>";
                        echo "PRODUCT WEIGHT IN KILO: " . $roundoff . "<br>";
                        $total = $subtotal_price + $addboxm;
                        $sizes = $addboxm;
                        echo "GRAND TOTAL: Php. " . $total . "<br><br>";
                    }
                    else
                    {
                        echo "TOTAL ITEMS PURCHASED (QTY): " . $qty . "<br>";
                        echo "SUBTOTAL (SUBTOTAL): Php. " . $subtotal_price . "<br>";
                        echo "Shipping Fee: ". $addboxl . "<br>";
                        echo "PRODUCT WEIGHT IN KILO: " . $roundoff . "<br>";
                        $total = $subtotal_price + $addboxl;
                        $sizes = $addboxl;
                        echo "GRAND TOTAL: Php. " . $total . "<br><br>";
                    }//round off

                } else {
                    // Since lbc_status == 1, this means all the items' lbc_type is set to "Pouch"
                    if($qty > 2) {
                        // Automatic Box
                        if($roundoff >=0 && $roundoff <=3)
                        {
                            echo "TOTAL ITEMS PURCHASED (QTY): " . $qty . "<br>";
                            echo "SUBTOTAL (SUBTOTAL): Php. " . $subtotal_price . "<br>";
                            echo "Shipping Fee: ". $addboxs . "<br>";
                            echo "PRODUCT WEIGHT IN KILO: " . $roundoff . "<br>";
                            $total = $subtotal_price + $addboxs;
                            $sizes = $addboxs;
                            echo "GRAND TOTAL: Php. " . $total . "<br><br>";
                        }
                        elseif($roundoff >=4 && $roundoff <=6)
                        {
                            echo "TOTAL ITEMS PURCHASED (QTY): " . $qty . "<br>";
                            echo "SUBTOTAL (SUBTOTAL): Php. " . $subtotal_price . "<br>";
                            echo "Shipping Fee: ". $addboxm . "<br>";
                            echo "PRODUCT WEIGHT IN KILO: " . $roundoff . "<br>";
                            $total = $subtotal_price + $addboxm;
                            $sizes = $addboxm;
                            echo "GRAND TOTAL: Php. " . $total . "<br><br>";
                        }
                        else
                        {
                            echo "TOTAL ITEMS PURCHASED (QTY): " . $qty . "<br>";
                            echo "SUBTOTAL (SUBTOTAL): Php. " . $subtotal_price . "<br>";
                            echo "Shipping Fee: ". $addboxl . "<br>";
                            echo "PRODUCT WEIGHT IN KILO: " . $roundoff . "<br>";
                            $total = $subtotal_price + $addboxl;
                            $sizes = $addboxl;
                            echo "GRAND TOTAL: Php. " . $total . "<br><br>";
                        } //round off

                    } else if($qty == 2) {
                        // Check first if there are 2 items in the cart, and not only 1 item having 2 qty
                        if($cart_size > 1) {
                            // Check if both items have same size
                            if($cart_array[0]['pouch_size'] == $cart_array[1]['pouch_size']) {


                                if($cart_array[0]['pouch_size'] == 1)
                                {
                                    echo "BOTH ITEMS HAVE SAME POUCH SIZE WHICH IS " . $cart_array[0]['pouch_size'] . "<br>";
                                    echo "TOTAL ITEMS PURCHASED (QTY): " . $qty . "<br>";
                                    echo "SUBTOTAL (SUBTOTAL): Php. " . $subtotal_price . "<br>";
                                    echo "Shipping Fee: ". $addpouchs . "<br>";
                                    echo "PRODUCT WEIGHT IN KILO: " . $roundoff . "<br>";
                                    $total = $subtotal_price + $addpouchs;
                                    $sizes = $addpouchs;
                                    echo "GRAND TOTAL: Php. " . $total . "<br>";
                                }
                                else
                                {
                                    echo "BOTH ITEMS HAVE SAME POUCH SIZE WHICH IS " . $cart_array[0]['pouch_size'] . "<br>";
                                    echo "TOTAL ITEMS PURCHASED (QTY): " . $qty . "<br>";
                                    echo "SUBTOTAL (SUBTOTAL): Php. " . $subtotal_price . "<br>";
                                    echo "Shipping Fee: ". $addpouchl . "<br>";
                                    echo "PRODUCT WEIGHT IN KILO: " . $roundoff . "<br>";
                                    $total = $subtotal_price + $addpouchl;
                                    $sizes = $addpouchl;
                                    echo "GRAND TOTAL: Php. " . $total . "<br>";
                                }//carr_array == 1

                            } else {
                                    echo "BOTH ITEMS HAVE DIFFERENT POUCH SIZE!" . "<br>";
                                    echo "ITEM 1 HAS POUCH SIZE OF " . $cart_array[0]['pouch_size'] . "<br>";
                                    echo "ITEM 2 HAS POUCH SIZE OF " . $cart_array[1]['pouch_size'] . "<br>";
                                    echo "TOTAL ITEMS PURCHASED (QTY): " . $qty . "<br>";
                                    echo "SUBTOTAL (SUBTOTAL): Php. " . $subtotal_price . "<br>";
                                    echo "Shipping Fee: ". $addpouchl . "<br>";
                                    echo "PRODUCT WEIGHT IN KILO: " . $roundoff . "<br>";
                                    $total = $subtotal_price + $addpouchl;
                                    $sizes = $addpouchl;
                                    echo "GRAND TOTAL: Php. " . $total . "<br>";
                            }
                        } else {
                            echo "THERE IS ONLY 1 ITEM WITH 2 QUANTITY (ORDERS)";

                        }
                    } else if($qty == 1) {
                        // This means there is only 1 item in the cart
                        echo "There is only 1 item in the cart";
                    }
                }

        } 
        elseif($ship_region == 'Mindanao')
        {
            echo "Mindanao";die;
        }
        else
        {
            echo "Visayas";die; 
        }

        $order = array(
            'date' => $date,
            'order_id' => $ord_id,
            'order_status' => '0',
            'product_id' => $item['id'],
            'product_name' => $item['name'],
            'qty' => $item['qty'],
            'transaction_num' => $transac_id,
            'username' => $this->session->userdata('email'),
            'start_price' => $item['start_price'],
            'price' => $item['price'],
            'total_price' => $item['price'] * $item['qty'],
            'city_state' => strip_tags($this->input->post('ship_region')),
            'shipping_address' => strip_tags($this->input->post('shipping_address')),
            'home_address' => strip_tags($this->input->post('home_address')),
            'additional' => $sizes,
            'ship_method' => 'Shipping',
        );

        $cust_id = $this->PaymentModel->insert_order_detail($order);

        $id = $this->db->where('serial', $item['id']);
        $bought = array(
            'bought' => $item['bought'] + $item['qty'],
            //'stock' => $item['stock'] - $item['qty'],
        );
        $this->db->update('products', $bought);
endforeach; endif;

1 个答案:

答案 0 :(得分:0)

问题出在外部和内部foreach循环中。两个循环都有相同的变量名$item并不好。然后两个具有相同名称的变量相互作用,这是不可取的。

你有类似的东西

    foreach ($cart as $item):
        ...
        foreach ($cart = $this->cart->contents() as $item)
        {
            you use inner $item;
        }
        you use outer $item;
    endforeach;

将内部$item重命名为$inner_item,将外部$item重命名为$outer_item,因此代码将

    foreach ($cart as $outer_item):
        ...
        foreach ($cart = $this->cart->contents() as $inner_item)
        {
            you use $inner_item;
        }
        you use $outer_item;
    endforeach;

错误的原因是内部循环到达数组的末尾,如果之后使用此变量,则内部$item始终是数组的最后一个元素。