Jump to content

Help with adding items from select menu into session


wolfinu

Recommended Posts

hello all, I was hoping someone could help me figure out how to add sizes to my shopping cart.

Right now the items add just fine. The site sells shirts and I'm having trouble displaying the sizes once selected and added to the cart. It will post the size selected, but if the customer tries to add another shirt the size will overwrite the last.

 

Any help is appreciated.

 

Heres how I add items to the cart...

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;

 

The form with the size selection and add to cart button..

  <form action="cart.php?action=add&product_id=<?php echo $row_rs_products['product_id'] ?>" method="POST" name="addcart" id="addcart">
      <table width="300" border="0">
        <tr>
          <td><label for="sizes"></label>
            <select name="product_size" id="product_size" title="<?php echo $row_rs_products['product_size']; ?>">
              <?php
do {  
?>
              <?php
} while ($row_rs_sizes = mysql_fetch_assoc($rs_sizes));
  $rows = mysql_num_rows($rs_sizes);
  if($rows > 0) {
      mysql_data_seek($rs_sizes, 0);
  $row_rs_sizes = mysql_fetch_assoc($rs_sizes);
  }
?>
            </select></td>
          <td><input type="submit" name="submit" id="submit" value="add to cart">

 

function displaying the 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> </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>'.$product_size.'</td>';
		$output[] = '<td>'.$_POST['product_size'].'</td>';
		//
		$output[] = '<td><input type="text" name="qty'.$product_id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td>X $'.($product_price * $qty).'</td>';
		$total += $product_price * $qty;
		$output[] = '<td><a href="cart.php?action=delete&product_id='.$product_id.'" class="r">Remove</a> </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>';

Link to comment
Share on other sites

I would use an array rather than a comma separated string. Problem you are having is you don't know what options(like shirt size) go with which product.

 

I have not test this but you should be able to get the idea

session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'empty':
	if($cart) {
		unset($cart);
	}
	break;
case 'add':
	$cart[] = array("product_id" => $_GET['product_id'],  "qty" => $qty, "size" => $size);
	break;

$_SESSION['cart'] = $cart;

 

By putting it into an array each product will have its own row in the array and you can also add options to the row as I have added above.

 

Now you should be able to loop through the array and show the cart

function showCart() {
global $db;
if (!empty($_SESSION['cart'])) {
	foreach ($_SESSION['cart'] as $item) {
               // show your results below
               echo $item['product_id'], $item['qty'], $item['size'];

               }
        }
}


 

Hope that helps

 

Ray

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.