0
case "remove":
        if(!empty($_SESSION["cart_item"]))
         {
            foreach($_SESSION["cart_item"] as $k => $v)
             {
                    if($_GET["id"] == $k)
                    {
                        unset($_SESSION["cart_item"][$k]);  
                        }           
                    if(empty($_SESSION["cart_item"]))
                    {
                        unset($_SESSION["cart_item"]);
                        }
            }
            header("location:cart.php");
        }
        break;

i m tring to delet item from session array for a shoping cart my function work ehrn i add singel value in cart but when i added multipal value my function stop working
my code is

2
Contributors
3
Replies
12
Views
2 Years
Discussion Span
Last Post by d_randhawa
0

Mind to show us how you store your $_SESSION["cart_item"]

Edited by lps

0
<?php
//include_once 'config/db-config.php';
//session_start();

session_start();
//require_once("dbcontroller.php");
//$db_handle = new DBController();
include_once 'config/db-config.php';
$db_handle = new DBConfig();

if(!empty($_GET["action"])) {
switch($_GET["action"]) {
    case "add":
        if(!empty($_POST["id"])) {
            $productByid = $db_handle->runQuery("SELECT * FROM tbl_product WHERE id='" . $_GET["id"] . "'");
            $itemArray = array($productByid[0]["id"]=>array('name'=>$productByid[0]["name"], 'id'=>$productByid[0]["id"],'pic'=>$productByid[0]["pic"], 'quantity'=>$_POST["quantity"], 'price'=>$productByid[0]["nprice"]));

            if(!empty($_SESSION["cart_item"])) {
                if(in_array($productByid[0]["id"],$_SESSION["cart_item"])) {
                    foreach($_SESSION["cart_item"] as $k => $v) {
                            if($productByid[0]["id"] == $k)
                                $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
                    }
                } else {
                    $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                }
            } else {
                $_SESSION["cart_item"] = $itemArray;
            }
        }
    break;

    case "remove":
        if(!empty($_SESSION["cart_item"]))
         {
            foreach($_SESSION["cart_item"] as $k => $v)
             {
                    if($_GET['id'] == $k)

                        session_unset($_SESSION["cart_item"][$k]);  

                    if(empty($_SESSION["cart_item"]))

                        session_unset($_SESSION["cart_item"]);

            }
            header("location:cart.php");
        }
        break;

    case "empty":
        unset($_SESSION["cart_item"]);
        header("location:cart.php");
    break;  
}
}

?>
0

this is my cart page where i m show my cart item

<?php
if(isset($_SESSION["cart_item"])){
    $item_total = 0;
     $item_totala = 0;
     $del=0;
     $itemid=0;
?>

    <section id="cart_items">
        <div class="container">
            <?php /*?><div class="breadcrumbs">
                <ol class="breadcrumb">
                  <li><a href="#">Home</a></li>
                  <li class="active">Shopping Cart</li>
                </ol>
            </div><?php */?>
            <div class="table-responsive cart_info">
                <table class="table table-condensed">
                    <thead>
                        <tr class="cart_menu">
                            <td class="image"><a href="shop2.php?action=empty&val=<?php echo $_SESSION['val'];?>&val1=<?php echo $_SESSION['val1'];?>">Empty Cart</a></td>
                            <td class="description">Name</td>
                            <td class="price">Price</td>
                            <td class="quantity">Quantity</td>
                            <td class="total">Total</td>
                            <td></td>
                        </tr>
                    </thead>
                    <tbody>


<?php

    foreach ($_SESSION["cart_item"] as $item){
        ?>

                        <tr>

<td  >                       </td>
                            <?php /*?><td class="cart_product">
                                <a href=""><img src="login/admin/product_img/<?php echo $item['pic'];?>" alt="image not found"></a>
                            </td><?php */?>
                            <td class="cart_description">

                                <h4><a href=""><?php echo $item['name'];?></a></h4>
                                <?php /*?><p><?php echo $item['id'];?></p><?php */?>
                            </td>
                            <td class="cart_price">
                            <?php $rs=$item['price'];?>
                                <p><?php echo $item['price'];?></p>
                            </td>

                            <td class="cart_quantity">
                                <div class="cart_quantity_button">
                                    <?php /*?><a class="cart_quantity_up" href=""> + </a><?php */?>
                                    <input  type="hidden" name="price" id="num1" value="<?php echo $item['price'];?>" autocomplete="off" size="2"/>
                                    <p><?php echo $item['quantity'];?></p>
                                    <?php /*?><a class="cart_quantity_down" href=""> - </a><?php */?>
                                </div>
                            </td>

<?php
       $item_total = ($item["price"]*$item["quantity"]);
         //$item_total += ($item["price"]*$item["quantity"]);
        $item_totala += $item_total;
        ?>

                            <td class="cart_total">

                                <p class="cart_total_price"><?php echo $item_total ?> </p>
                            </td>
                            <td class="cart_delete">

                                <a href="shop2.php?action=remove&id=<?php echo $item["id"]; ?>" class="cart_quantity_delete" ><i class="fa fa-times"></i></a>
                            </td>
                        </tr>

<?php 
}

//header('Location:shop.php?val=$val &val1=$val1');
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.