*URGENT shopping cart help

Discussion in 'PHP' started by vassili, Feb 12, 2008.

  1. #1
    i made a shopping cart through the tutorial but i can't post the link...you can google shopping cart and watchmakerproject to get the tutorial.

    so, i'm having problems adding my own fields into the cart. there's a function to show the contents of the cart and i'm having problems understanding how to add some extra "size" columns where users input quantities which adds up to the total quantity. right now, all the size fields are set to the '.qty.' value as i don't know how to proceed.

    changing the "size" quantity does nothing when i press update and changing the "total" quantity set all the "size" quantities to the "total" quantity.

    here is the my function:

    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[] = '<table align="center" cellpadding="0" border="0">';
    		$output[] = '<tr><td>';
    		$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
    		$output[] = '<table align="center" cellpadding="10" width="820" border="0">';
    		$output[] = '<tr><td></td>
    					<td align="center">Style</td>
    					<td align="center">Colour</td>
    					<td align="center">Sizes</td>
    					<td align="center">Total</td>
    					<td align="center">Price</td>
    					<td align="center">Subtotal</td></tr>';
    					
    		foreach ($contents as $id=>$qty) {
    			$sql = "SELECT * FROM item, stock WHERE item.stock_id=stock.stock_id AND uniq_ID='$id'";
    			$result = $db->query($sql);
    			$row = $result->fetch();
    			extract($row);
    			$output[] = '<tr>';
    			$output[] = '<td style="border:1px solid #999;"><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
    			$output[] = '<td width="100" style="border:1px solid #999;" align="center">'.$item_ID.'</td>';
    			$output[] = '<td width="150" style="border:1px solid #999;" align="center">'.$color.'</td><td>';
    			$output[] = '
    						<[COLOR="Red"]table align="center"><tr>
    						<td valign="top" align="center">'.$size1.'</td>
    	 					<td valign="top" align="center">'.$size2.'</td>
    	 					<td valign="top" align="center">'.$size3.'</td>
    	 					<td valign="top" align="center">'.$size4.'</td>
    	 					<td valign="top" align="center">'.$size5.'</td>
    	 					<td valign="top" align="center">'.$size6.'</td>
    	 					<td valign="top" align="center">'.$size7.'</td>
    	 					<td valign="top" align="center">'.$size8.'</td>
    						</tr>
    	 					<tr>
    	 					<td valign="top" align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="1" maxlength="3" /></td>
    	 					<td valign="top" align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="1" maxlength="3" /></td>
    	 					<td valign="top" align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="1" maxlength="3" /></td>
    	 					<td valign="top" align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="1" maxlength="3" /></td>
    	 					<td valign="top" align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="1" maxlength="3" /></td>
    	 					<td valign="top" align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="1" maxlength="3" /></td>
    	 					<td valign="top" align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="1" maxlength="3" /></td>
    	 					<td valign="top" align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="1" maxlength="3" /></td>
    	 					
    	 					</tr></table></td>';[/COLOR]			
    			
    			
    			$output[] = '<td width="50" style="border:1px solid #999;"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
    			$output[] = '<td style="border:1px solid #999;">$'.$price.'</td>';
    			$output[] = '<td style="border:1px solid #999;">$' . number_format($price * $qty, 2) . '</td>';
    			
    			$total += $price * $qty;
    			$output[] = '</tr>';
    		}
    		$output[] = '</table>';
    		
    		$output[] = '<p align="right">Subtotal: <strong>$'. number_format($total,2).'</strong></p>';
    		$output[] = '<div align="right"><button type="submit" id="mysubmit">Update cart</button></div>';
    		$output[] = '<p align="right"><a href="memberindex.php" >Back to Shopping...</a></p>
    ';
    		$output[] = '</form>';
    	} else {
    		$output[] = '<p>Your cart is empty.</p>';
    	}
    		
    		$output[] = '</td></tr>';
    		$output[] = '</table>';
    	 	return join('',$output);
    	 
    	 	
    }
    Code (markup):
    and here is the contents of cart.php, the page the shopping cart displays on.

    <?php
    
    session_start();
    
    if (@$_SESSION['auth'] != "yes") 
    {
    	header("Location: index.html");
    	exit();
    }
    
    // Process actions
    $cart = $_SESSION['cart'];
    $action = $_GET['action'];
    
    switch ($action) {
    	case 'add':
    		if ($cart) {
    			$cart .= ','.$_GET['id'];
    		} else {
    			$cart = $_GET['id'];
    		}
    		break;
    	case 'delete':
    		if ($cart) {
    			$items = explode(',',$cart);
    			$newcart = '';
    			foreach ($items as $item) {
    				if ($_GET['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')) {
    				$id = str_replace('qty','',$key);
    				$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
    				$newcart = '';
    				foreach ($items as $item) {
    					if ($id != $item) {
    						if ($newcart != '') {
    							$newcart .= ','.$item;
    						} else {
    							$newcart = $item;
    						}
    					}
    				}
    				for ($i=1;$i<=$value;$i++) {
    					if ($newcart != '') {
    						$newcart .= ','.$id;
    					} else {
    						$newcart = $id;
    					}
    				}
    			}
    		}
    	}
    	$cart = $newcart;
    	break;
    }
    $_SESSION['cart'] = $cart;
    $id=$_GET['id'];
    $dealer=$_SESSION['dealer'];
    
    echo ShowCart();
    
    ?>
    Code (markup):
    i display the cart by simply echoing the querystring result from ShowCart();

    when i add stuff and update stuff, it goes to the switch cases...something i don't get either...

    can someone explain how i can get the extra size fields to work please? thanks in advance.
     
    vassili, Feb 12, 2008 IP
  2. vassili

    vassili Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    the red part of the showcart() function is the new input i added. what do i need to do tot he case "add" and case "update" to allow it to work?

    i'd really appreciate the help here
     
    vassili, Feb 12, 2008 IP