Jump to content

Looping Over an Array (help)


seaweed

Recommended Posts

Can anyone point out why this isn't incrementing the qty of items already in the cart? Maybe it's a Friday Brain Fart...

 


session_start();

if (isset($_SESSION['CART']) == false || isset($_SESSION['CART']['ITEMS']) == false) {

       $_SESSION['CART']['ITEMS'] = array();

}


foreach ($_POST AS $sku => $qty) {

if (!empty($qty)) {

        // if the product sku is already in the cart, do this //

	foreach ($_SESSION['CART']['ITEMS'] as $cart_item => $item) {

		if ($item['item_sku'] == $sku) {

		       $_SESSION['CART']['ITEMS'][$cart_item]['item_qty'] = $_SESSION['CART']['ITEMS'][$cart_item]['item_qty'] + $qty;
		       $found = true;
		       break;

		}

	}

	// if the product sku is not already in the cart, do this //

	if (!$found) {
	     $_SESSION['CART']['ITEMS'][] = array('item_sku' => $sku, 'item_qty' => $qty);
	} 
}

}

Link to comment
https://forums.phpfreaks.com/topic/205888-looping-over-an-array-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.