Jump to content

Archived

This topic is now archived and is closed to further replies.

neolumi

Custom Shopping cart problems

Recommended Posts

Hi people, I've been having problems with a script I'm making for a shopping cart. It basically runs off cookies and mysql. Its fine sometimes but whenever you go to add more than two or sometimes one item it throws back errors.

The only post data is from a product page being its item code and quantity wanted, heres an example of cookie data that causes an error:
2*5^1:3^1*13
layout is: num of items * itemcode ^ quantity : 2nditem... * total cost

what happens is when it comes to putting out a list it somethings returning the wrong data as it counts the correct number of items by finding the ^ but count didnt seem to work. Help Appreciated!!

I think its easier to post the whole code as opposed to select chunks:
[code]<?php
require("inc/conn.php");
$item_code = $_POST['ic'];
$quant = $_POST['quantity'];

if(isset($_POST['add'])){
$sql = "SELECT product_price,product_title,product_stock FROM tcom_products WHERE product_code = ".$item_code;
if ($result = mysql_query($sql)) {
$row = mysql_fetch_array($result);
$cost = floatval($row['product_price']);
}else{
exit("item not found");
$cost = 0;
}

if(isset($_COOKIE['tcomscart7stk'])){

$bits = explode("*", $_COOKIE['tcomscart7stk']);
if(substr_count($bits[1], ":") != 0){
$citems = explode(":", $bits);
}else{
$citems[0] = $bits[1];
}
$count = count($citems)-1;

for ($i = 0; $i <= $count; $i++) {
$iitem = explode("^", $citems[($i)]);

if($iitem[0] == $item_code){
$found = true;

if(($iitem[1] + $quant) > $row['product_stock']){
$nb0 = intval($bits[0]);
$cost = 0;
$act = "ne";
}else{
$nb0 = intval($bits[0]) + $quant;
$x = $quant + $iitem[1];
$citems[$i] = $item_code."^".$x;
$act = "q";
}

}

}

if(!isset($found)){
$nb0 = intval($bits[0]) + $quant;
$citems[($count+1)] = $item_code."^".$quant;
$act = "n";
}

$nb2 = floatval($bits[2]) + (floatval($cost) * intval($quant));

$rcitems = implode(":", $citems);
$assemble = $nb0."*".$rcitems."*".$nb2;
setcookie("tcomscart7stk",$assemble,time()+60*60*24*30, "/");
}else{
$tcost = floatval($cost) * intval($quant);
$assemble = $quant."*".$item_code."^".$quant."*".$tcost;
setcookie("tcomscart7stk",$assemble,time()+60*60*24*30, "/");
}
header("Location: shop_basket.php?m=$act");
}

if($_GET['m'] == "n"){
$display = "Item added to your basket.";
}elseif($_GET['m'] == "q"){
$display = "Item quantity updated.";
}elseif($_GET['m'] == "ne"){
$display = "Sorry, you cannot add that many of the same item due to stock levels.";
}
if(isset($display)){print $display;}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>The Toy Chest of Maidstone :: Online Shop</title>
<link rel="stylesheet" type="text/css" href="inc/styles.css" />
<link rel="stylesheet" type="text/css" href="inc/shop.css" /></head>

<body>
<br/><br/><br/><br/>
<table border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" width="243" ><table border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#F3F3F3"><img src="shared/nav_top.gif" alt="The Toy Chest of Maidstone Logo" /></td>
</tr>
<tr>
<td style="background:url('shared/nav_bg.gif') #FFFFFF">
<?php include("nav.php");
require("inc/conn.php"); ?>
</td>
</tr>
<tr>
<td height="38" style="background:url('shared/nav_bottom.gif')" valign="top" align="center"><img src="shared/clearpx.gif" height="3" alt="" /><br/>
<form style="margin: 0px;" method="post" action="search.php">
<input name="" value="" type="text" size="16" style="height: 16px;" />&nbsp;<input name="Search!" value="Search!" type="submit" />
</form></td>
</tr>
</table></td>
<td valign="top"><table border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="background:url('shared/content_top.gif')"> </td>
<td style="background:url('shared/content_topr.gif')"><img src="shared/clearpx.gif" height="11" width="6" alt="" /></td>
</tr>
<tr>
<td style="background:url('shared/content_bg.gif')" align="center"><img src="shared/clearpx.gif" height="5" alt="" /><br/>
<table width="476" border="0" cellspacing="0" cellpadding="0">
<tr>
<td id="header"><img src="shared/header_shop.gif" alt="Online Shop" width="224" height="48" /></td>
</tr>
<tr>
<td><img src="shared/clearpx.gif" height="5" alt="" /></td>
</tr>
<tr>
<td>
<b>Shopping Basket:</b>
</td></tr>
<tr><td><br/><form action="" method="post" name="basketsubmit">
<table width="460" border="0" cellspacing="2" cellpadding="2" class="basket" align="center">
<tr>
<td class="baskethead" style="text-align: left;" height="24">&nbsp;Item Name:</td>
<td class="baskethead" width="80">Quantity:</td>
<td class="baskethead" width="80">Price:</td>
</tr>
<?php

if(isset($_COOKIE['tcomscart7stk'])){

$bits = explode("*", $_COOKIE['tcomscart7stk']);
$subcount = substr_count($bits[1], "^");
if($subcount == 1){
$citems[0] = $bits[1];
}else{
$citems = explode(":", $bits);
}
$count = count($citems);
$rowcol = 2;

for ($i = 1; $i <= $subcount; $i++) {
$i2 = $i - 1;
$iitem = explode("^", $citems[$i2]);

if($iitem[0] != ""){

$sql = "SELECT p.product_title,p.product_category,p.product_price,p.product_was,p.product_stock,p.product_code,p.product_age,p.product_pic,p.product_desc,c.cat_id,c.cat_name,b.brand_id,b.brand_name,b.brand_website FROM tcom_products p,tcom_category c ,tcom_brand b WHERE p.product_code = ".$iitem[0]." AND c.cat_id = p.product_category AND b.brand_id = p.product_brand";
if ($result = mysql_query($sql)) {
$row = mysql_fetch_array($result);
}else{exit("<br/>sql".$iitem[0]);}


if($rowcol == 2){
$rowcol = 1;
}else{
$rowcol = 2;
}

print '<tr class="row'.$rowcol.'">
<td>&nbsp;<b><a href="shop_product.php?ic='.$iitem[0].'" title="'.$row['product_title'].'">'.$row['product_title'].'</a></b><div class="fine">&nbsp;&nbsp;Brand: '.$row['brand_name'].' :: Item Code: '.str_pad($row['product_code'], 4, "0", STR_PAD_LEFT).'</div></td>
<td align="center"><input name="quantity" type="text" size="2" maxlength="2" style="text-align:center;" value="'.$iitem[1].'" /></td>
<td align="center">&pound;'.floatval($row['product_price']).'</td>
</tr>';

}

}
}else{
print '<tr class="row1">
<td colspan="3" style="text-align: center;">There are currently no items in your basket, please click <b><a href="shop.php" title="Shop Front">here</a></b> to go to the shop front.</td></tr>';
$bits[2] = "0.00";
}
?>
<tr class="baskethead">
<td colspan="2" align="right"><b>Sub-total:</b></td>
<td align="center"><b>&pound;<?php print $bits[2]; ?></b></td>
</tr>
</table>
</form>
<br/><span class="fine">All Payments are processed securely by PayPal and do not require a paypal account.</span>
</td>
</tr>
<tr>
<td><img src="shared/clearpx.gif" height="10" alt="" /></td>
</tr>
</table>
</td>
<td style="background:url('shared/content_mbgr.gif')"></td>
</tr>
<tr>
<td style="background:url('shared/content_b.gif')"><img src="shared/clearpx.gif" width="498" height="17" alt="" /></td>
<td style="background:url('shared/content_br.gif')"></td>
</tr>
</table></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>[/code]

Share this post


Link to post
Share on other sites

×

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.