2016-07-25 62 views
0

我把死在我的代碼看看什麼是輸出是正確的,輸出在這裏。 http://prntscr.com/bxam1f那麼問題是當我把它插入表中。它插入到表格中,但只有1個名稱是最後一行要插入的名稱。這是http://prnt.sc/bxanpe插入數據到表

MY CODES IS HERE

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; 

回答

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始終是數組的最後一個元素。

+0

謝謝你一直在想這個近一個星期覺得壓抑繼續看到這個錯誤只是爲了找出$ item正在與其他項目互動。即時通訊只是新的編程,這就是爲什麼需要練習更多。再次感謝@vonoak – erinr

+0

我的榮幸。樂意效勞:-) –