Padma Rubhan
Padma Rubhan

Reputation: 293

I'm just trying to remove the single item from a session[cart_item]

I used a session for adding an item in my page without database. here i have one problem while trying to remove this file in my page. here is my code for link for remove. I used to select data by its title variable.

<a href="step3.php?action=remove&title=<?php echo $item["title"]; ?>" class="product-title">
    <span class="label label-warning pull-right">Remove</span>
</a>

I used switch case to select remove operation.

case "remove":
    if(!empty($_SESSION["cart_item"])) {
        foreach($_SESSION["cart_item"] as $k => $v) {
            if($_GET["title"] == $k)
                unset($_SESSION["cart_item"][$k]);              
            if(empty($_SESSION["cart_item"]))
                unset($_SESSION["cart_item"]);
        }
    }
break;

it select the title variable perfectly. but remove action is not performing.

here also i dont have any unique value for select an single data from session... give some solutions too..

Upvotes: 0

Views: 67

Answers (4)

Anto S
Anto S

Reputation: 2449

In your URL you are passing action and title no code.

So it should be

case "remove":
    if(!empty($_SESSION["cart_item"])) {
        foreach($_SESSION["cart_item"] as $k => $v) {
            if($_GET["title"] == $k)
                unset($_SESSION["cart_item"][$k]);              
            if(empty($_SESSION["cart_item"]))
                unset($_SESSION["cart_item"]);
        }
    }
break;

Upvotes: 0

Ananthakumar
Ananthakumar

Reputation: 323

You have to pass parameter "code" or else "cart_item" should be empty but your verifying it should not be empty in first if condition if(!empty($_SESSION["cart_item"])) verify your condition:

case "remove":
    if(!empty($_SESSION["cart_item"])) { //condition 1
        foreach($_SESSION["cart_item"] as $k => $v) {
                if($_GET["code"] == $k)
                    unset($_SESSION["cart_item"][$k]);              
                if(empty($_SESSION["cart_item"]))// condition 2 wont work 
                    unset($_SESSION["cart_item"]);
        }
    }
break;

Upvotes: 1

RysQ
RysQ

Reputation: 348

You haven't pass $_GET["code"] through the link. Shouldn't it be $_GET["title"] == $k ?

Upvotes: 0

dev0
dev0

Reputation: 1057

if($_GET["code"] == $k) should be if($_GET["title"] == $k)

Upvotes: 0

Related Questions