0

I have this code to delete any idea from a cart... I need to delete two$_SESSION. 1) $_SESSION["cart_array"]

2 $_SESSION["minicart"]

Without me adding $_SESSION["minicart"] it does delete the $_SESSION["cart_array"] but when i added it i got the minicart part i got an undefined index: minicart. So I

tried

if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) && !isset($_SESSION["minicart"]) || count($_SESSION["minicart"]) < 1) {

the code above checks // If the cart session variable is not set or cart array is empty*

to the orginal if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {

So

<?php

 //   if user wants to remove an item from cart
 if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
    // Access the array and run code to remove that array index
    $key_to_remove = $_POST['index_to_remove'];
    if (count($_SESSION["cart_array"]["minicart"]) <= 1) {
        unset($_SESSION["cart_array"]["minicart"]);
    } else {
        unset($_SESSION["cart_array"]["minicart"] ["$key_to_remove"]);
        sort($_SESSION["cart_array"]["minicart"]);
    }
}
?>

My quesion Looking at the tried what I am i doing wrong in the if statement and also what am I doing wrong in the statement to delete the ($_SESSION["cart_array"]) AND ($_SESSION["minicart"])

If this is still unclear please leave a comment and I will do my best to explain it again.

Amy
  • 55
  • 10
  • just test for the existance of the key prior to trying to unset it. `if (isset($_SESSION["cart_array"]["minicart"]) && count($_SESSION["cart_array"]["minicart"]) <= 1)` – Orangepill Jul 15 '13 at 18:47
  • @Orangepill i still got `undefined index:minicart` in line 73 where `if (count($_SESSION["cart_array"]["minicart"]) <= 1) {` is – Amy Jul 15 '13 at 18:50
  • It should be `if (isset($_SESSION["cart_array"]["minicart"]) && count($_SESSION["cart_array"]["minicart"]) <= 1)` – Orangepill Jul 15 '13 at 18:52
  • That's where you must ask `isset()` first. Otherwise you will access a nonexistant index with the count function. – Sven Jul 15 '13 at 18:52
  • You could better use the `empty`-function. If you use `count` on an empty array/string, it will return the `undefined index` error. – Pieter Jul 15 '13 at 18:52
  • @Orangepill i still got undefined index:minicart in line 73 where if (count($_SESSION["cart_array"]["minicart"]) <= 1) { – Amy Jul 15 '13 at 18:54
  • If you use the isset first then the second half of the statement will not be run because of logical short circuiting. If the left hand side of a && expression results in false php knows that the whole expression will be false regardless of the outcome of the right hand side so the right hand side is never evaluated. – Orangepill Jul 15 '13 at 18:55
  • @Pieter i used empty and they error is gone BUT now the code to delete the item which is the code to `// if user wants to remove an item from cart` is not working :'( – Amy Jul 15 '13 at 18:56
  • @amy this error was addressed on the third comment. – Orangepill Jul 15 '13 at 18:58
  • @Orangepill i dont understand what u mean – Amy Jul 15 '13 at 19:11
  • Your code should read `if (isset($_SESSION["cart_array"]["minicart"]) && count($_SESSION["cart_array"]["minicart"]) <= 1)` to avoid that error – Orangepill Jul 15 '13 at 19:11
  • do u want me to post the full code to my page? it about 213 lines including the htmls – Amy Jul 15 '13 at 19:27

2 Answers2

1

Try changing

if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {

to

if (isset($_POST['index_to_remove']) && ($_POST['index_to_remove'])) {

or !empty instead of isset

Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
Niket Malik
  • 1,075
  • 1
  • 14
  • 23
  • i did this but its not deleting the item anymore – Amy Jul 15 '13 at 18:58
  • Try for `!empty` instead of isset – Niket Malik Jul 15 '13 at 19:00
  • i need t use `isset` seems am posting something from a `form` when the delete button has been press – Amy Jul 15 '13 at 19:10
  • First of all why you are adding isset for `$_POST['index_to_remove']` twice? And are you posting the form parameters properly? – Niket Malik Jul 15 '13 at 19:15
  • `blo` yes I am... the php code i did does `// Access the array and run code to remove that array index` – Amy Jul 15 '13 at 19:24
  • Have you tried with just `if (isset($_POST['index_to_remove']) {` rather than `if (isset($_POST['index_to_remove']) && ($_POST['index_to_remove'])) {` – Niket Malik Jul 15 '13 at 19:27
  • i try it and it 50% works... the problem is that it doesn't delete the `['minicart']` session and 2) when i refresh the page i get a `underfined index:minicart` – Amy Jul 15 '13 at 19:31
  • Try `if (isset($_POST['index_to_remove']) && (!empty($_SESSION["minicart"]))) {` – Niket Malik Jul 15 '13 at 19:39
  • still getting the same error on `if (count($_SESSION["cart_array"]["minicart"]) <= 1) {` – Amy Jul 15 '13 at 20:21
  • I change the `count` to `!empty` and i dont get the error but `['minicart']` session haven't been deleted – Amy Jul 15 '13 at 20:23
  • `if (isset($_POST['index_to_remove']) && (!empty($_SESSION["cart_array"]["minicart"]))) {` – Niket Malik Jul 15 '13 at 20:33
  • okay the cart it is not showing any error. I have check if the both the `cart_array` and `minicart` session are empty using `if(isset($_SESSION['minicart']['cart_array']) && !empty($_SESSION['minicart']['cart_array'])) { echo ("I am not empty"); }` although the cart looks clear. The `cart_array` and `minicart` sessions is not deleted – Amy Jul 15 '13 at 20:42
  • How about `session_destroy()` rather than unset? – Niket Malik Jul 15 '13 at 20:49
  • `session_destroy($_SESSION["cart_array"]["minicart"]);` not working – Amy Jul 15 '13 at 20:56
  • I really can't figure out the problem with the code, if there is no need of any session if the statement is true, the just add `session_destroy()`, it will destroy all the session.. – Niket Malik Jul 15 '13 at 20:58
  • I am echoing `` and `minicart` do you think that why it is still showing? – Amy Jul 15 '13 at 20:59
  • yes thats why unset will be suitable because I am only removing the session variable that is getting posted – Amy Jul 15 '13 at 21:01
  • Just a final shot, try `if (exist($_SESSION["cart_array"]) && ($_SESSION["cart_array"] <= 1)) { unset($_SESSION["cart_array"]); }` and in else statement remove space between mini_cart and `$key_to_remove` and also put `$key_to_remove` as `'$key_to_remove'` – Niket Malik Jul 15 '13 at 21:16
  • No, basically replace the second if and else part as `if (exist($_SESSION["cart_array"]) && ($_SESSION["cart_array"] <= 1)) { unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["minicart"]['$key_to_remove']); sort($_SESSION["cart_array"]); }` – Niket Malik Jul 15 '13 at 21:24
  • still nothing... do u want me to post my full code on this question? maybe i did something wrong that i cant find – Amy Jul 15 '13 at 21:30
  • u r not in the discussion room – Amy Jul 15 '13 at 21:50
  • @bio The problem has been fixed – Amy Jul 16 '13 at 05:59
  • just realised I haven't fix it. If I have more than 1 item in the cart `cart_array` only shows `1`. It should count all the items in the arrays and because of this if I delete 1 item from the cart it deleted everything – Amy Jul 16 '13 at 13:23
0
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) 
    && !isset($_SESSION["minicart"]) || count($_SESSION["minicart"]) < 1) {

You have a boolean logic problem here. This is not preventing $_SESSION['minicart'], or $_SESSION['cart_array'] for that matter, from being accessed if it is not set. That accessing when not set is what is giving you your error. Check your negations (!), and whether you really want || there.

I don't understand what all you need to check before unsetting those $_SESSION values, I don't have enough information and context. As such, I can't give you code to copy in paste into yours. All I can do is tell you why you're getting undefined index errors. Your if statement, which is supposed to keep $_SESSION['cart_array'] and $_SESSION['minicart'] from being accessed if they aren't set, is not doing what you intended. Trace through it and check your logic. If you can't make it work, then simplify it by breaking that into multiple, nested if statements.

If you want copy and pastable code, you will need to clarify what your doing a bit more. What are you checking with the count() parts, under what circumstances do you want to unset variables, and which variables do you want to unset.

  • u r missing about 3 `)` – Amy Jul 15 '13 at 19:22
  • this just completely stop an item getting add to my cart – Amy Jul 15 '13 at 20:44
  • Okay, you were focusing on the wrong part of my answer, so I changed it. Check your if statements, they're not doing what you intend them to do. – John Morris Jul 15 '13 at 22:05
  • I fixed it my way but dont know if it is the right way by doing this... I deleted all the `['minicart']` from this pages so only session variable `['cart_array']` left then where i calculated the total price i did `$cartTotal = $minicart = money_format("%n", (double)$cartTotal);` and i put `minicart` in a session ` $_SESSION['smallcart']=$minicart;` – Amy Jul 17 '13 at 06:55