Jump to content

Help with shopping cart. Almost there!


dachshund

Recommended Posts

To add items to my shopping cart the user must select a size and then click 'add to basket'.

 

When they do this is adds the item and corresponding size to a string in this format 1s2 where 1 is the id and 2 is the size.

 

Multiple items would look like this 1s2,2s1,1s2

 

That's all fine. The problem I am now having is getting the code to recognise 1s2 exists twice in the string and putting the $qty to 2 for that item.

 

Here is my current code. Any help on this would be HUGELY appreciated as it's doing my head in!

 

Thanks!

 


<div id="view_basket">
			<ul>
			<?php
			$basket = $_SESSION['basket'];

			if ($basket) {
    			        $data = preg_split("#[s,]#", $basket);
    			        $data = array_chunk($data, 2);
    			
    			        foreach($data as $key => $val)
    			
        		        $contents[] = (isset($contents[$data])) ? $contents[$data] + 1 : 1;
			echo '<form action="basket.php?action=update" method="post">';
			echo '<table>';
			foreach ($data 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($rows);
			?>

				<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>
<?php
}
?>

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.