Jump to content

Adding item and size to basket


dachshund

Recommended Posts

OK, so I have a form which submits the id of an item on a shopping site as well as the size selected by the customer.

 

Size S/M is 1

Size M/L is 2

 

To get what has been submitted when they click "Add To Basket" i have this code:

 

$sizechoice = $_POST['sizechoice'];
$basket = $_SESSION['basket'];
$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($basket) {
		$basket .= ','.$_GET['id'].'-'.$sizechoice;
	} else {
		$basket = $_GET['id'];
	}
}

 

I used to have it like that, but minus $sizechoice, so the basket was just ids like 1,5,3,2. Now each id is accompanied by a size choice, so it would be 1-1,5-1,3-2,2-1 for example.

 

How can I later separate the id from the size again using the dash?

 

Thanks,

 

Jack

Link to comment
Share on other sites

Are you storing basket as 1 long string?

 

For example, if I ordered items 1 3 and 7, and ordered sizes S/M, S/M, and M/L, would "basket" become a string reading "1-1, 3-1,7-2"?

 

If so, might I suggest using an array instead of a string?

 

It would be easier to store items using:

 

$_SESSION['basket'][$item_id] = $item_size

So for example, my orders of 1, 3, 7 above would work out like so:

 

echo '1-'.$_SESSION['basket'][1]; // "1-1"
echo '3-'.$_SESSION['basket'][3]; // "3-1";
echo '7-'.$_SESSION['basket'][7]; // "7-2";

 

Is it possible for you to change your program to do something like this? Would you like helping changing it?

Link to comment
Share on other sites

yes, that's right. they are being stored as one string. it would be great if you could help me change my code to what you suggested.

 

at the moment i have some code which adjusts the string, depending on whether they add, update, or remove an item. the code is:

 


$sizechoice = $_POST['sizechoice'];
$basket = $_SESSION['basket'];
$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($basket) {
		$basket .= ','.$_GET['id'].'-'.$sizechoice;
	} else {
		$basket = $_GET['id'];
	}
	break;
case 'delete':
	if ($basket) {
		$items = explode(',',$basket.'-'.$sizechoice);
		$newbasket = '';
		foreach ($items as $item) {
			if ($_GET['id'] != $item) {
				if ($newbasket != '') {
					$newbasket .= ','.$item;
				} else {
					$newbasket = $item;
				}
			}
		}
		$basket = $newbasket;
	}
	break;
case 'update':
if ($basket) {
	$newbasket = '';
	foreach ($_POST as $key=>$value) {
		if (stristr($key,'qty')) {
			$id = str_replace('qty','',$key);
			$items = ($newbasket != '') ? explode(',',$newbasket) : explode(',',$basket);
			$newbasket = '';
			foreach ($items as $item) {
				if ($id != $item) {
					if ($newbasket != '') {
						$newbasket .= ','.$item;
					} else {
						$newbasket = $item;
					}
				}
			}
			for ($i=1;$i<=$value;$i++) {
				if ($newbasket != '') {
					$newbasket .= ','.$id;
				} else {
					$newbasket = $id;
				}
			}
		}
	}
}
$basket = $newbasket;
break;
}
$_SESSION['basket'] = $basket;

 

To display each item and the item's quantity in the basket I have the code:

 


$basket = $_SESSION['basket'];
			if ($basket) {
			$items = explode(',',$basket);
			$contents = array();
			foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
			}
			echo '<form action="basket.php?action=update" method="post">';
			echo '<table>';
			foreach ($contents as $id=>$qty) {
			$sql = "SELECT * FROM store WHERE id LIKE '$id' AND live LIKE '0'";
			$result = mysql_query($sql);
			while ($rows = mysql_fetch_array($result))	{
			extract($row);

 

thanks!

 

Link to comment
Share on other sites

sorry, i the top part of the code is actually

 


$basket = $_SESSION['basket'];
$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($basket) {
		$basket .= ','.$_GET['id'];
	} else {
		$basket = $_GET['id'];
	}
	break;
case 'delete':
	if ($basket) {
		$items = explode(',',$basket);
		$newbasket = '';
		foreach ($items as $item) {
			if ($_GET['id'] != $item) {
				if ($newbasket != '') {
					$newbasket .= ','.$item;
				} else {
					$newbasket = $item;
				}
			}
		}
		$basket = $newbasket;
	}
	break;
case 'update':

 

i haven't worked out how to do the $sizechoice part yet. but hopefully you can help :)

Link to comment
Share on other sites

Without knowing more about the incomming information I really can't provide a solution to you.

 

Can you run your script as-is and copy the HTML from the page source? Without seeing the HTML form that will be submitted on an edit, add, or delete, it's not possible to provide you with a suggestion that is likely to work.

Link to comment
Share on other sites

ok, here is the whole function, minus some sensitive paypal parts

 

<?php
function showBasket() {
?>
<div id="view_basket">
			<ul>
			<?php
			$basket = $_SESSION['basket'];
			if ($basket) {
			$items = explode(',',$basket);
			$contents = array();
			foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
			}
			echo '<form action="basket.php?action=update" method="post">';
			echo '<table>';
			foreach ($contents as $id=>$qty) {
			$sql = "SELECT * FROM store WHERE id LIKE '$id' AND live LIKE '0'";
			$result = mysql_query($sql);
			while ($rows = mysql_fetch_array($result))	{
			extract($row);
			?>

				<li>
					<div id="view_basket_image">
					<img src="<?php echo $rows['indeximage']; ?>" />
					</div>

					<div id="view_basket_title">
					<span class="view_basket_brand"><?php echo $rows['brand']; ?> ·</span> <span class="view_basket_description"><?php echo $rows['title']; ?></span>
					</div>

					<div id="view_basket_qty">
					<input type="text" name="qty<?php echo $id; ?>" value="<?php echo $qty; ?>" size="3" maxlength="3" class="view_basket_qty" />
					</div>

					<div id="view_basket_price">
					<span class="view_basket_x">x</span>£<?php echo $rows['price']; ?>
					<?php
					/* WORK OUT ITEM WEIGHTS */
					$itemweight = $rows['weight'] * $qty;
					$totalweight += $rows['weight'] * $qty;
					?>
					</div>

					<div id="view_basket_itemtotal">
					<?php $itemtotalprice = $rows['price'] * $qty; $itemtotal = number_format($itemtotalprice, 2, '.', ','); echo '&pound'; echo $itemtotal;
					$total += $rows['price'] * $qty;
					?>
					</div>
					<div class="clear"></div>
				</li>
				<?php
				}
				}
				?>
			</ul>
		</div>

		<form action="" method="post">
		<div id="update_basket">
			<button type="submit" class="addtobasket">Update basket</button>
		</div>
		</form>

		<div class="clear"></div>

		<div id="shipping_and_total">

			<div id="grand_total">
				<div id="left_column_header">
				Select your region
				</div>
				<div id="region_select">


				<form name="region" action="">
					<select class="checkout_select" name="region" onchange="this.form.submit()">
						<option>-</option>
						<option <?php
						$region = $_GET['region'];
						$ukshipping = $_GET['ukshipping'];

						if (!$ukshipping == '')	{
						$region = 'United Kingdom';
						}

						if ($region == 'United Kingdom')	{ echo 'selected="yes"'; }
						?>>United Kingdom</option>
						<option <?php
						if ($region == 'Europe')	{ echo 'selected="yes"'; }
						?>>Europe</option>
						<option <?php
						if ($region == 'Worldwide')	{ echo 'selected="yes"'; }
						?>>Worldwide</option>
					</select>
				</form>
				</div>

				<?php
				if ($region == 'United Kingdom' OR $region == 'Europe' OR $region == 'Worldwide')	{
				?>
				<div id="left_column_header">
				Select shipping
				</div>

				<div id="region_select">
				<form name="shipping" action="?region=United Kingdom">
					<select class="checkout_select" name="ukshipping" onchange="this.form.submit()">
						<?php
						if ($region == 'United Kingdom')	{
						?>
						<option>-</option>
						<option <?php
						if ($ukshipping == '1-2 Days - 5.00 GBP')	{ echo 'selected="yes"'; }
						?>>1-2 Days - 5.00 GBP</option>	
						<option <?php
						if ($ukshipping == '3-4 Days - Free')	{ echo 'selected="yes"'; }
						?>>3-4 Days - Free</option>	
						<?php
						}
						?>						
					</select>
				</form>
				</div>

				<?php
				}
				if ($ukshipping == '3-4 Days - Free')	{
				$shippingcost = 0;
				}

				if ($ukshipping == '1-2 Days - 5.00 GBP')	{
				$shippingcost = 5;
				}

				$grandtotal = $total + $shippingcost;
				?>

				<div id="left_column_header">
				Total weight
				</div>

				<div id="total_price">
				<?php
				echo $totalweight; ?>
				</div>

				<div id="left_column_header">
				Total price 
				</div>

				<div id="total_price">
				£<?php
				$grandtotal = number_format($grandtotal, 2, '.', ',');
				echo $grandtotal; ?>
				</div>
			</div>
		</form>
		</div>

		<div class="clear"></div>			


		<div id="update_basket">

		<?php
		$a=1;
		$b=1;
		$c=1;
		$basket = $_SESSION['basket'];
		if ($basket) {
		$items = explode(',',$basket);
		$contents = array();
		foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}
		foreach ($contents as $id=>$qty) {
		$sql = "SELECT * FROM store WHERE id LIKE '$id'";
		$result = mysql_query($sql);
		while ($rows = mysql_fetch_array($result))	{
		extract($row);
		?>

		<input type="hidden" name="item_name_<?php echo $a; ++$a; ?>" value="<?php echo $rows['brand'].' · '.$rows['title']; ?>">
		<input type="hidden" name="amount_<?php echo $c; ++$c; ?>" value="<?php echo $rows['price']; ?>">
		<input type="hidden" name="quantity_<?php echo $b; ++$b; ?>" value="<?php echo $qty; ?>">

		<?php
		}
		}
		}
		?>

		<input type="hidden" name="shipping_1" value="<?php echo $shippingcost; ?>">

		<?php if ($ukshipping == '' OR $ukshipping == '-')	{
		?>
		<div class="addtobasket_hidden">Checkout with Paypal</div>
		<?php
		}else	{
		?>
		<button type="submit" name="submit" class="addtobasket">Checkout with Paypal</button>
		<?php
		}
		?>
		</form>
		</div>

		<div class="clear"></div>

		<?php
		}else	{
		echo '<p>You shopping basket is empty.</p>';
}
return join('',$output);
}		

Link to comment
Share on other sites

Hi Jack

 

I have built something similar recently, I loaded all applicable elements the user should submit into a form on the ordering page, I then hooked up a cart.php script to it which processed the submission of the form in the order.php form.

Where the size was concerned in my HTML I created a <option></option> menu with the values of S, M, L etc in there and assigned a certain ID to each one through a variable of $sizeID which information was stored in my database basically my client charged an extra $0.50 for large etc so the variable would add or deduct from the price...anyway back onto topic; within my cart.php all of the variables which were processed through order.php were inserted into an array and then pushed through a foreach loop, at the end of the loop I used a variable of $pushOrder to echo the order in a table row, this meant when the user was to view their basket they would be able to have a visual representation of everything they had ordered.

 

If you would like to see some examples of my script I would be happy to post them up?

 

Hope you can relate to what I have described, sorry if you feel it is irrelevant to your system

 

Alex.

Link to comment
Share on other sites

hi,

 

thanks for your help. my basket currently does show them everything they have selected, the quantity of the item, an image, shipping prices depending on where they need it shipped to etc.

 

on the item page i also have a drop down with S, M, L.

 

the inventory and sizes available are stored in the database for each item.

 

the problem I have is storing the selected size in the session. currently it is storing the product id, but i need it to store the product id and the selected size of that product.

 

does that make sense?

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.