Modified cart functions v2

/** Cart Functions **/
function add($p_id,$name,$price,$qty,$image){

	if(!inCart($p_id)){
		$temp['id'] = $p_id;
		$temp['name'] = $name;
		$temp['price'] = $price;
		$temp['image'] = $image;
		$temp['qty'] = $qty;
		$temp['sub_tot'] = $price*$qty;

		$_SESSION['cart'][] = $temp;
		$_SESSION['total'] = $_SESSION['total']+$temp['sub_tot'];
		$_SESSION['tot_qty'] = $_SESSION['tot_qty']+$qty;
	} else {
		update($p_id,$_SESSION['cart'][$key]['qty']+$qty);
	}

}

function inCart($p_id){
	$temp = $_SESSION['cart'];
	if(is_array($temp) and !empty($temp)){
		foreach($temp as $key => $value){
			if($value['id'] == $p_id){
				return true;
			}
		}
	}
	return false;
}

function disp(){
	$temp = $_SESSION['cart'];
	if(is_array($_SESSION['cart'])){
	echo '<ul>';
 	foreach($temp as $key => $value){
		echo '<li>?Name '.$value['name'].' Price '.$value['price'].' x '.$value['qty'].' = '.$value['sub_tot'].'</li>';
	}
	echo '<li>Total = '.$_SESSION['total'].' Qty = '.$_SESSION['tot_qty'].'</li>'; 
	echo '</ul>';
	} else {
		echo 'Empty!';
	} 
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s