Saurabh Sharma Saurabh Sharma - 5 months ago 14
SQL Question

cart that show item with remove button for each of them

i want to create shopping cart with simple php and mysql code here is one code to view the cart and i want to see the item in a table and with a remove button for each
here i created a cart and seesion is there i want a simple cart so can anyone tell me how i can display a remove button on each item and also to show the item in the shopping cart



<?php
session_start();
include_once("config.php");




if(empty($_SESSION['cart']))
$_SESSION['cart'] = array();
if(isset($_POST['product_code'])){
$product_code = $_POST["product_code"];
$product_name = $_POST["product_name"];
$product_qty = $_POST["product_qty"];
$product_price = $_POST["price"];
$product_color = $_POST["product_color"];
$_SESSION['cart'][] = array('name' => $product_name, 'product_code' => $product_code, 'qnty' => $product_qty, 'color' =>$product_color, 'price' => $product_price);



}

print_r ($_SESSION['cart']);/*header("Location: view_cart.php");*/

if(isset($_POST['empty']) ){

unset($_SESSION["cart"]);

}


?>
<html>
<head>
</head>
<body>
<form method="POST" action="cart.php"><div align="center"><button type="remove" name="empty" >empty</button></div>
<a href="ppage.php">back </a>
<table width="100%" cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>Price</th><th>code</th><th>colour</th><th>remove</th></tr></thead>
<tbody>

<?php



foreach($_SESSION['cart'] as $itm=>$val)
echo "<thead>";
echo"<tr>";
echo '<td>'.$val['$qnty'].'</td>';
echo '<td>'.$val['name'].'</td>';
echo '<td>'.$val['price'].'</td>';
echo '<td>'.$val['product_code'].'</td>';
echo '<td>'.$val['color'].'</td>';
/*echo '<td>'<a href="?remove=<?php echo $itm; ?>">remove</a>'</td>'
*/

if (isset($_POST['remove'])){

unset($_SESSION['cart'][$_POST['remove']]);}
/*echo <div align="right"><button type="remove" name="remove" >remove</button></div> */
echo '</tr>';
echo "<thead>";



?>
echo <div align="right"><button type="remove" name="remove" >remove</button></div>
echo '<td>'<a href="?remove=<?php echo $itm; ?>">remove</a>'</td>'

</form>
</tbody>
</body>
</html>




Answer

There's no need to create any kind of form there, just create a hyperlink for each row and append the array index value to it. And during processing, simply catch the array index value using $_GET superglobal and then delete the item array from the $_SESSION['cart'].

So your code should be like this:

<?php
    session_start();

    if(empty($_SESSION['cart'])){
        $_SESSION['cart'] = array();
    }

    if(isset($_POST['product_code'])){
        $product_code = $_POST["product_code"];
        $product_name = $_POST["product_name"];
        $product_qty = $_POST["product_qty"];
        $product_price = $_POST["price"];
        $product_color = $_POST["product_color"];
        $_SESSION['cart'][] = array('name' => $product_name, 'product_code' => $product_code, 'qnty' => $product_qty, 'color' =>$product_color, 'price' => $product_price); 
    }

    if(isset($_GET['remove']) && (!empty($_GET['remove'] || $_GET['remove'] == 0))){
        unset($_SESSION['cart'][$_GET['remove']]);
    }

?>
<html>
    <head>

    </head>
    <body>

        <table width="100%" cellpadding="6" cellspacing="0" border="1" style="text-align:center;">
        <thead>
            <tr>
                <th>Quantity</th>
                <th>Name</th>
                <th>Price</th>
                <th>code</th>
                <th>colour</th>
                <th>remove</th>
            </tr>
        </thead>
        <tbody>
        <?php
        if(count($_SESSION['cart'])){
            foreach($_SESSION['cart'] as $itm=>$val){
                echo '<tr>';
                echo '<td>'.$val['qnty'].'</td>';
                echo '<td>'.$val['name'].'</td>';
                echo '<td>'.$val['price'].'</td>';
                echo '<td>'.$val['product_code'].'</td>';
                echo '<td>'.$val['color'].'</td>';
                echo '<td><a href="?remove=' . $itm . '">remove</a></td>';
                echo '</tr>';
            }
        }else{
            echo '<tr><td colspan="6">No item in the cart</td></tr>';
        }
        ?>                              
        </tbody>
        </table>

    </body>
</html>