Help with adding sizes to cart page

hello everyone,
I’ve been racking my brain with this…
I’m trying to display the sizes of shirts when added to my cart.
The sizes come from a select menu.
The sizes show up in the cart, but overwrite the last size added.

For example …
add shirt1 size-small
add shirt2 size-medium

the cart displays…
shirt 1 size-medium
shirt 2 size-medium

Can you please help me so when I add a new item it doesn’t overwrite the previous size.

all help is appreciated

my code is as follows…

selection from my product page

<select name="shirt_size">
<option value="x-small">x-small</option>
<option value="small">small</option>
<option value="medium">medium</option>
<option value="large">large</option>
</select>

code that displays shirt size

$output[] = '<td>'.$_POST['shirt_size'].'</td>';

function displaying my cart

function showCart() {
	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">';
		$output[] = '<table>';
			// start div
			$output[] = '<div id="cart_table">';
			//
			$output[] = '<tr>';
			$output[] = '<td><h4>Product</h4></td>';
			$output[] = '<td><h4>Item No.</h4></td>';
			$output[] = '<td><h4>Price</h4></td>';
			$output[] = '<td><h4>Size</h4></td>';
			$output[] = '<td><h4>Quantity</h4></td>';
			$output[] = '<td><h4>Price Total</h4></td>';
			$output[] = '<td><h4>&nbsp;</h4></td>';
			$output[] = '</tr>';
			//new row
		foreach ($contents as $product_id=>$qty) {
			$sql = 'SELECT * FROM products WHERE product_id = '.$product_id;
			$result = $db->query($sql);
			$row = $result->fetch();
			extract($row);
			//
			$output[] = '<tr>';
			$output[] = '<td><a href="product.php?product_id='.$product_id.'">'.$product_title.'</a></td>';
			$output[] = '<td>'.$product_plu.'</td>';
			$output[] = '<td>$'.$product_price.'</td>';
			//
			$output[] = '<td>'.$_POST['shirt_size'].'</td>';
			//
			$output[] = '<td><input type="text" name="qty'.$product_id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
			$output[] = '<td>X&nbsp;$'.($product_price * $qty).'</td>';
			$total += $product_price * $qty;
			$output[] = '<td><a href="cart.php?action=delete&product_id='.$product_id.'" class="r">Remove</a>&nbsp;</td>';
			$output[] = '</tr>';
			//end div
			$output[] = '</div>';
			
			
			
		}
		$output[] = '</table>';
		$output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>';
		$output[] = '<div class="float-right"><button type="submit">Update cart</button>';
		$output[] = '</form>';

Show us the code for adding something to the cart.

sorry bout that


session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
	case 'empty':
		if($cart) {
			unset($cart);
		}
		break;
	case 'add':
		if ($cart) {
			$cart .= ','.$_GET['product_id'];
		} else {
			$cart = $_GET['product_id'];
		}
		break;
	case 'delete':
		if ($cart) {
			$items = explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($_GET['product_id'] != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			$cart = $newcart;
		}
		break;
	case 'update':
	if ($cart) {
		$newcart = '';
		foreach ($_POST as $key=>$value) {
			if (stristr($key,'qty')) {
				$product_id = str_replace('qty','',$key);
				$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
				$newcart = '';
				foreach ($items as $item) {
					if ($product_id != $item) {
						if ($newcart != '') {
							$newcart .= ','.$item;
						} else {
							$newcart = $item;
						}
					}
				}
				for ($i=1;$i<=$value;$i++) {
					if ($newcart != '') {
						$newcart .= ','.$product_id;
					} else {
						$newcart = $product_id;
					}
				}
			}
		}
	}
	$cart = $newcart;
	break;
}
$_SESSION['cart'] = $cart;

Okay… you’re not storing size information in the cart. Only thing you’re storing is the product ID. So the system doesnt know what size the person ordered.

I tried this… but I still can’t get it right.
Any idea what I’m doing wrong?


 case 'add':
        if ($cart) {
            $cart .= ','.$_GET['product_id']['shirt_size'];
        } else {
            $cart = $_GET['product_id']['shirt_size'];
        }
        break;