Jump to content

Is there something I can replace foreach with


huntrguy102

Recommended Posts

I have this code

function showCart5() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
	foreach ($contents as $id=>$qty) {
		$sql = 'SELECT * FROM books WHERE id = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);

		$total += $price * $qty;	
		$output[] = '<strong> $'.$total.'</strong>';

	}
} else {
	$output[] = ' ';
}
return join('',$output);
}

 

which is basically posting the final price.  I am working on a book store and when someone puts in more than 1 different book. it posts the price of the first book and then the total price of both books.  I think its a problem with the foreach part of the function. Is there a way I can replace the foreach with something else that will just post the final total?

 

here is the link to the bookstore http://www.chulainnlibros.com/cart/index.php . If you try it your self you may have any idea of what I'm talking about.

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.