Jump to content

Can't figure this out.


holladb

Recommended Posts

Can somebody help me?

 

I'm trying to display the $color with the corresponding $id.

 

My cart is setup like $id-$color,

So the items become separated by commas and the variables become separated by dashes.

 

I'm in crunch time on this project, the website needs to be live tomorrow so it is up for black friday. Please help!

 

Below is my code:

 

function showCart() {
global $db;
$cart = $_SESSION['cart'];
print($cart);
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$new = explode('-',$item);
		list($id, $color) = $new;
		$contents[$id] = (isset($contents[$id])) ? $contents[$id] + 1 : 1;
		$contents[$id] = $contents[$color];
		$content .= $color;
	}
	$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
	$output[] = '<table>';
	foreach ($contents as $id=>$qty) {
		foreach ($contents as $id=>$co) {
		print_r($contents);
		echo $co;
		$sql = 'SELECT * FROM products WHERE cereal = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<tr>';
		$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
		$output[] = '<td>'.$row['itemcode'].' - '.$co.'</td>';
		$output[] = '<td>$'.$row['pricemiddle'].'</td>';
		$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td>$'.($row['pricemiddle'] * $qty).'</td>';
		$total += $row['pricemiddle'] * $qty;
		$output[] = '</tr>';
	}
	}
	$output[] = '</table>';
	$output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>';
	$output[] = '<div><input type="image" src="images/buttons/update.gif" border="0"> <a href="checkout.php"><img src="images/buttons/checkout.gif" border="0"></a></div>';
	$output[] = '</form>';
} else {
	$output[] = '<p>Your shopping cart is empty.</p>';
}
return join('',$output);
}

Link to comment
https://forums.phpfreaks.com/topic/78107-cant-figure-this-out/
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.