Adding sizes to webshop

Hi! I’m currently working on a wepshop and i’m stuck at the point where i need to choose a size for the product and when i add the product to the cart till will show what size i have choosen.

This is the code where i have the size selection:
[php]<?php
if (!defined(‘WEB_ROOT’)) {
exit;
}

$product = getProductDetail($pdId, $catId);

// we have $pd_name, $pd_price, —$pd_size,— $pd_description, $pd_image, $cart_url
extract($product);
?>

<?php echo $pd_name; ?> <?php echo $pd_name; ?>
Price : <?php echo displayAmount($pd_price); ?>

Size:
<?php $sql = "SELECT pd_name, pd_size, pd_description, pd_price, pd_image, pd_qty FROM tbl_product WHERE pd_id = $pdId"; $result = mysql_query($sql) or print ("Can't select product id from table tbl_product.
" . $sql . "
" . mysql_error()); while($row = mysql_fetch_array($result)) { $product = getProductDetail($pdId, $catId); extract($product); $options = $pd_size; $pd_size = explode(",", $options); //plockar isär den ihopsatta storlekssträngen som lagrats i databasen till separata objekt ?>
	<?php }; ?>     
		<form action="library/cart-functions.php" method="POST">
    	<select name="pd_size" id="size">
    	<?php 
    		foreach($pd_size as $value) //foreach-loopen fixar en option-tag runt varje värde av storlekarna 
    		{
    	?>
      
      	[b]<option name="size" value="<?php echo $value; ?>"><?php echo $value; ?></option>[/b]
    
		<?php
   		 }
   		 ?>
    	</select>



<?php // if we still have this product in stock // show the 'Add to cart' button if ($pd_qty > 0) { ?> <?php } else { echo "".'Out Of Stock'.""; } ?>
<?php echo $pd_description; ?>
[/php]

And then i have cart.php and in that cart-functions.php. This is cartfunctions:
[php]<?php
require_once ‘config.php’;

/*********************************************************

  •             SHOPPING CART FUNCTIONS 
    

*********************************************************/

function addToCart()
{
// make sure the product id exist
if (isset($_GET[‘p’]) && (int)$_GET[‘p’] > 0) {
$productId = (int)$_GET[‘p’];
} else {
header(‘Location: index.php’);
}

// does the product exist ?
$sql = "SELECT pd_id, pd_qty
        FROM tbl_product
		WHERE pd_id = $productId";
$result = dbQuery($sql);

if (dbNumRows($result) != 1) {
	// the product doesn't exist
	header('Location: cart.php');
} else {
	// how many of this product we
	// have in stock
	$row = dbFetchAssoc($result);
	$currentStock = $row['pd_qty'];

	if ($currentStock == 0) {
		// we no longer have this product in stock
		// show the error message
		setError('The product you requested is no longer in stock');
		header('Location: cart.php');
		exit;
	}

}		

// current session id
$sid = session_id();

// check if the product is already
// in cart table for this session
$sql = "SELECT pd_id
        FROM tbl_cart
		WHERE pd_id = $productId AND ct_session_id = '$sid'";
$result = dbQuery($sql);

if (dbNumRows($result) == 0) {
	// put the product in cart table
	$sql = "INSERT INTO tbl_cart (pd_id, ct_size, ct_qty, ct_session_id, ct_date)
			VALUES ($productId, [b]'M'[/b], 1, '$sid', NOW())";
	$result = dbQuery($sql);
} else {
	// update product quantity in cart table
	$sql = "UPDATE tbl_cart 
	        SET ct_qty = ct_qty + 1
			WHERE ct_session_id = '$sid' AND pd_id = $productId";		
			
	$result = dbQuery($sql);		
}	

// an extra job for us here is to remove abandoned carts.
// right now the best option is to call this function here
deleteAbandonedCart();

header('Location: ' . $_SESSION['shop_return_url']);				

}

/*
Get all item in current session
from shopping cart table
*/
function getCartContent()
{
$cartContent = array();

$sid = session_id();
$sql = "SELECT ct_id, ct.pd_id, ct_size, ct_qty, pd_name, pd_price, pd_thumbnail, pd.cat_id
		FROM tbl_cart ct, tbl_product pd, tbl_category cat
		WHERE ct_session_id = '$sid' AND ct.pd_id = pd.pd_id AND cat.cat_id = pd.cat_id";

$result = dbQuery($sql);

while ($row = dbFetchAssoc($result)) {
	if ($row['pd_thumbnail']) {
		$row['pd_thumbnail'] = WEB_ROOT . 'images/product/' . $row['pd_thumbnail'];
	} else {
		$row['pd_thumbnail'] = WEB_ROOT . 'images/no-image-small.png';
	}
	$cartContent[] = $row;
}

return $cartContent;

}

/*
Remove an item from the cart
*/
function deleteFromCart($cartId = 0)
{
if (!$cartId && isset($_GET[‘cid’]) && (int)$_GET[‘cid’] > 0) {
$cartId = (int)$_GET[‘cid’];
}

if ($cartId) {	
	$sql  = "DELETE FROM tbl_cart
			 WHERE ct_id = $cartId";

	$result = dbQuery($sql);
}

header('Location: cart.php');	

}

/*
Update item quantity in shopping cart
*/
function updateCart()
{
$cartId = $_POST[‘hidCartId’];
$productId = $_POST[‘hidProductId’];
$itemQty = $_POST[‘txtQty’];
$numItem = count($itemQty);
$numDeleted = 0;
$notice = ‘’;

for ($i = 0; $i < $numItem; $i++) {
	$newQty = (int)$itemQty[$i];
	if ($newQty < 1) {
		// remove this item from shopping cart
		deleteFromCart($cartId[$i]);	
		$numDeleted += 1;
	} else {
		// check current stock
		$sql = "SELECT pd_name, pd_qty
		        FROM tbl_product 
				WHERE pd_id = {$productId[$i]}";
		$result = dbQuery($sql);
		$row    = dbFetchAssoc($result);
		
		if ($newQty > $row['pd_qty']) {
			// we only have this much in stock
			$newQty = $row['pd_qty'];

			// if the customer put more than
			// we have in stock, give a notice
			if ($row['pd_qty'] > 0) {
				setError('The quantity you have requested is more than we currently have in stock. The number available is indicated in the &quot;Quantity&quot; box. ');
			} else {
				// the product is no longer in stock
				setError('Sorry, but the product you want (' . $row['pd_name'] . ') is no longer in stock');

				// remove this item from shopping cart
				deleteFromCart($cartId[$i]);	
				$numDeleted += 1;					
			}
		} 
						
		// update product quantity
		$sql = "UPDATE tbl_cart
				SET ct_qty = $newQty
				WHERE ct_id = {$cartId[$i]}";
			
		dbQuery($sql);
	}
}

if ($numDeleted == $numItem) {
	// if all item deleted return to the last page that
	// the customer visited before going to shopping cart
	header("Location: $returnUrl" . $_SESSION['shop_return_url']);
} else {
	header('Location: cart.php');	
}

exit;

}

function isCartEmpty()
{
$isEmpty = false;

$sid = session_id();
$sql = "SELECT ct_id
		FROM tbl_cart ct
		WHERE ct_session_id = '$sid'";

$result = dbQuery($sql);

if (dbNumRows($result) == 0) {
	$isEmpty = true;
}	

return $isEmpty;

}

/*
Delete all cart entries older than one day
*/
function deleteAbandonedCart()
{
$yesterday = date(‘Y-m-d H:i:s’, mktime(0,0,0, date(‘m’), date(‘d’) - 1, date(‘Y’)));
$sql = “DELETE FROM tbl_cart
WHERE ct_date < ‘$yesterday’”;
dbQuery($sql);
}

?>[/php]

and this is cart.php:
[php]<?php
require_once ‘library/config.php’;
require_once ‘library/cart-functions.php’;

$action = (isset($_GET[‘action’]) && $_GET[‘action’] != ‘’) ? $_GET[‘action’] : ‘view’;

switch ($action) {
case ‘add’ :
addToCart();
break;
case ‘update’ :
updateCart();
break;
case ‘delete’ :
deleteFromCart();
break;
case ‘view’ :
}

$cartContent = getCartContent();
$numItem = count($cartContent);

$pageTitle = ‘Shopping Cart’;
require_once ‘include/header.php’;

// show the error message ( if we have any )
displayError();

if ($numItem > 0 ) {
?>

" method="post" name="frmCart" id="frmCart"> <?php $subTotal = 0; for ($i = 0; $i < $numItem; $i++) { extract($cartContent[$i]); $productUrl = "index.php?c=$cat_id&p=$pd_id"; $subTotal += $pd_price * $ct_qty; ?> <?php } ?>
Item Unit Price Quantity Total   Size
<?php echo $pd_name; ?> <?php echo displayAmount($pd_price); ?> <?php echo displayAmount($pd_price * $ct_qty); ?> ';" class="button"> [b]<?php print $ct_size; ?>[/b]
Sub-total <?php echo displayAmount($subTotal); ?>  
Shipping <?php echo displayAmount($shopConfig['shippingCost']); ?>  
Total <?php echo displayAmount($subTotal + $shopConfig['shippingCost']); ?>  
 
<?php } else {

?>

<?php //require_once 'include/top.php'; ?>

 

You shopping cart is empty

If you find you are unable to add anything to your cart, please ensure that your internet browser has cookies enabled and that any other security software is not blocking your shopping session.

<?php } $shoppingReturnUrl = isset($_SESSION['shop_return_url']) ? $_SESSION['shop_return_url'] : 'index.php'; ?> <?php if ($numItem > 0) { ?> <?php } ?>
<?php include 'include/footer2.php'; ?>
[/php]

So, what im looking for is a way to get the choice made from the red part of the first code into a variable i can put instead of the blue M in cart-functions.php so that i can store that value in my database table so that the green part in cart.php exstracts the right sixe from the database. Rigt now it only puts a M wichever size I choose because i dont knoe the right thing to put there. I hope this makes sence to someone and that that someone is willing to help! :smiley:

If you want to check it out in real life, the adress is http://sannakarlin.com/butik/index.php. THANKS!

Sponsor our Newsletter | Privacy Policy | Terms of Service