Edgar Ong Edgar Ong - 4 months ago 6
PHP Question

Inserting data to the table

i put die in my code to look what is the output which is correct and the output is here. http://prntscr.com/bxam1f then the problem is when i insert it to the table. it inserted to the table but in just 1 name which is the last row to be inserted. this is the 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;

Answer

The problem is in outer and inner foreach loops. It is not good to have the same variable name $item for both loops. Then both variables with same name interact, which is undesirable.

You have something like this

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

Rename inner $item for example to $inner_item and for clearness outer $item to $outer_item, so code will be

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

The reason for your error is because inner loop goes to the end of an array and if you use this variable after that, in inner $item is always last element of an array.