Saurabh Sharma
Saurabh Sharma

Reputation: 51

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>

Upvotes: 2

Views: 1475

Answers (1)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

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, like this:

echo '<td><a href="?remove=' . $itm . '">remove</a></td>';

And during processing, simply catch the array index value using $_GET superglobal and then delete the item array from the $_SESSION['cart'], like this:

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

So your code should be like this:

<?php
    // Start session
    session_start();

    // Declare $_SESSION['cart'] as an empty array
    if(empty($_SESSION['cart'])){
        $_SESSION['cart'] = array();
    }

    // Store all product details in $_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); 
    }

    // Check whether the URL parameter 'remove' is set or not 
    // If it's set and not empty, then delete that item array from the $_SESSION['cart']
    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
        // Check whether the cart is empty or not
        if(count($_SESSION['cart'])){
            // Loop through the $_SESSION['cart'] array
            // and display the item details and 'remove' hyperlink
            // for each row
            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> 

Upvotes: 1

Related Questions