Jump to content

Removal of items from cart removes more than I want!

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Cyberen

Cyberen

    Newbie

  • Members
  • PipPip
  • 19 posts
Whenever I have multiple items in this shopping cart, if I don't remove the items starting from the latest-added item down (the highest rem&id=Number) , it removes others I don't ask for. What can I do to fix the removal of these items? I think it might be a glitch in the code.

Here's the button that removes an item from the cart:

<? echo '<a class="sub" href="viewcart2.php?back=' . urlencode($_GET['back']) . '&action=rem&id=' . $i . '">Remove</a> '; ?>

Here's the function that removes the item from the cart. Is not unsetting all the $_session values a bad thing? It certainly doesn't remove it from the $_SESSION array if you print one out at the bottom.

<?

ini_set('display_errors','Off'); 

include 'db.php';


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]); 

  if($_GET['action']=='rev'){

   header("Location: pro.php?id=" . $items[0]);

   exit;  

  }

 }

 $val=$_SESSION['cart'][$i];

 $items= split("\|\|",$val);

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

  $num_items++;

 }

}

?>

And here's the loop that creates items from the database:

<?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);
mysql_free_result($result);
?>
<?= stripslashes($line['name'])
}
?>

I have the same problem on two different versions of the webpage which you can see below. The main difference is that viewcart5.php adds more $_SESSION values to the array, and I thought by unsetting them like I did with $_SESSION['cart'] the problems would go away, but the same problem involving removing items persists.
I don't know how common this problem is but I will provide any information I can.

Attached Files



#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
In your code you do quite a few things I question about, for example you check for $_GET['rev'] twice and not $_GET['rem'], also you unset the session variable outside of any of those IF statements, is that what you meant for it to do?

What is the split() call for? If your items are in the [$i] portion, unsetting the whole [$i] (unset($_SESSION['cart'][$i]);) will delete any other items in the cart, not just the one you want to.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Cyberen

Cyberen

    Newbie

  • Members
  • PipPip
  • 19 posts
I'm working off the code I was given, so please go in-depth with your criticism of it so I may learn something and perhaps even improve what the more experienced one did.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users