SITUATION:
Managed to make items removable from the cart and the array.
PROBLEM:
$_SESSION['numberofitems'] and $num_items are not equivalent at the end of the page; if I have 3 items in the cart and remove the first item (array/$i value 0) it removes two instead. So obviously the order of removing items must be from the last array entry down.
I SEEK:
An answer to why the code does the above, suggestions of what to look for.
Below is the code, I trimmed out the unimportant parts. Note the calculation for removal of items on top and the echoing of the two values I mentioned on the bottom, $_SESSION['numberofitems'] always ends up unequal to the $num_items which messes up what items are displayed. If the correct amount isn't displayed it gives me a blank item that costs 0$ and a nonfunctioning removal of item button.
Thanks for reading!
Code: Select all
<? //the two lines below should be on all pages
//error_reporting(E_ALL);
ini_set('display_errors','On');
include 'db.php';
$g = 0;
$c = 0;
session_start();
header("Cache-control: private");
$num_items=0;
for($i=0;$i<sizeof($_SESSION['cart']);$i++){
if(($_GET['action']=='rem'||$_GET['action']=='rev') && $_GET['id']==$i){
if($_GET['action']=='rev'){
$val=$_SESSION['cart'][$i];
$items= split("\|\|",$val);
}
unset($_SESSION['cart'][$i]);
$_SESSION['numberofitems']=$num_items;
if (($num_items)>0){
$num_items--;
}
else {session_destroy();}
unset($_SESSION['shiplength']);
unset($_SESSION['shipwidth']);
unset($_SESSION['shipheight']);
unset($_SESSION['shipweight']);
unset($_SESSION['shiplength2']);
unset($_SESSION['shipwidth2']);
unset($_SESSION['shipheight2']);
unset($_SESSION['shipweight2']);
//unset($_SESSION['itemdata']);
if($_GET['action']=='rev'){
header("Location: pro.php?id=" . $items[0]);
exit;
}
}
$val=$_SESSION['cart'][$i];
$items= split("\|\|",$val);
if($items[0]!=""){
$num_items++;
}
}
?>
<!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>
<link href="NewVersion.css" rel="stylesheet" type="text/css" />
<link rel="icon" type="image/ico" href="images/favicon.ico" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-6579606-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();
</script>
<script type="text/javascript" src="Scripts/dropdownscript.js"></script>
</head>
<body>
<div id="container">
<div id="header">
<br class="clearfloat" />
<table><tr><td align="left">
<a href="index.html" onmouseover="image1.src=loadImage1.src;" onmouseout="image1.src=staticImage1.src;">
<img name="image1" src="images/topbuttons/name1.png" border=0></a>
</td><td align="right"><img src="images/topbuttons/phone.gif" alt="1-866-JEZEBEL"/></td></tr>
<tr><td align="center" colspan="2">
<script type="text/javascript">
var menu=new menu.dd("menu");
menu.init("menu","menuhover");
</script>
</td></tr></table>
<!-- end #header --></div>
<div id="mainContent">
<!-- end #mainContent --></div>
<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
<!-- Customization Code -->
<!-- Remove leading // to activate custom variables -->
<script language="Javascript">
//var DOCUMENTGROUP='';
var DOCUMENTNAME='Add to Cart';
var ACTION='02';
</script>
<!-- End of Customization Code -->
<!-- Tracking Code v3.01 - All rights reserved -->
<script language="javascript1.1" src="http://www.jezebelgallery.com/tracking.js"></script><noscript>
<img src="http://webstats.silverscopepromotion.com/p.pl?a=1000638354890&js=no" width="1" height="1"></noscript><!--//-->
<!-- End of Tracking Code -->
<div id="container">
</tr>
<!-- bottom -->
<tr>
<td> Your cart contains
<?= $num_items ?>
item(s)<br /><br />
<a href="<?= $_GET['back']?>"><img src="images/topbuttons/shopping.gif" alt="Continue Shopping" /></a><hr /><br />
<?PHP
$total=0;
for($i=0;$i<sizeof($_SESSION['cart']);$i++){
$val=$_SESSION['cart'][$i];
$items= split("\|\|",$val);
if($items[0]!=""){
$query = "SELECT *,category from products,categories where categories.id=products.pid and products.id=" . $items[0];
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$line = mysql_fetch_array($result, MYSQL_ASSOC);
//if (isset($line)){
mysql_free_result($result);
?>
<!-- I am unable to add a thumbnail to the product because the $_POST id for the thumbnails of each individual item is the same as the category's thumbnail, which means I have to edit the database in order to change that and I (nor CrystalTech I suspect) know how to alter a MYSQL database to that extent. -->
<br />
<hr />
<span class="style1">
<!-- <img src="<?PHP //echo $line['thumbnail']?>" alt="product" /><br /> -->
<?= stripslashes($line['category']).":";?>
</span>
<?= stripslashes($line['name']) ?>
<br />
<span class="style1">Price:</span> $
<?
if($line['ceiling']=='Y' && $line['static']=='N'){
$query = "SELECT * from ceiling where deleted=0 and pid=" . $items[0];
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$ceil_cost = mysql_fetch_array($result, MYSQL_ASSOC);
$line['price']=$ceil_cost['h' . $ceiling[0]];
mysql_free_result($result);
}
echo number_format($line['price'],2);
// Select one of the following options to modify this item<br /> -->
echo '<br />';
echo '<a class="sub" href="viewcart5.php?back=' . urlencode($_GET['back']) . '&action=rem&id=' . $i . '"><img src="https://www.jezebelgallery.com/images/topbuttons/removefromcart.gif" alt="Remove" /></a> '; ?><br />
<?
$total+=$line['price'];
}
?>
</td>
</tr>
</body>
</html>
<?PHP
echo $num_items."<br />";
echo $_SESSION['numberofitems']."<br />";
print_r($_SESSION);
/* Closing connection */
mysql_close($link);
?>