Jump to content

Removing item from shopping cart


Eddietse91

Recommended Posts

I am creating an ecommerce website for a project work, it will not be used live. I am having trouble deleting items from the shopping cart! please help me

 

This is to add items to the shopping cart:

 


$cart_row = array(

    'item' => $item,

    'unitprice' => $price,

    'quantity' => $quantity

);

 

if (!isset($_SESSION['cart']))

    $_SESSION['cart'] = array();

 

$_SESSION['cart'][] = $cart_row;

 

var_dump($_SESSION);


 

 

 

This is to display the shopping cart and I want a remove button, I think it's incorrect though.

 

if (!isset($_SESSION['cart']) || (count($_SESSION['cart']) == 0)) {

    echo '<p>Your cart is empty.</p>';

} else {

    echo '<table border="1">

    <tr><th>Item</th><th>Unit price</th><th>No. of units</th><th>Subtotal</th></tr>';

    $total = 0;

    

    foreach($_SESSION['cart'] as $item) {

    

        echo "<tr><td>{$item['item']} </td>

       

<td>\${$item['unitprice']}</td><td>{$item['quantity']}</td>

        <td>$".($item['unitprice'] * $item['quantity'])."</td>

<td>. $items[$item] . ' (<a href="' . $_SERVER['PHP_SELF'] . '?remove=' . $item . '">Remove</a>)        </td>

 

</tr>";

        $total += ($item['unitprice'] * $item['quantity']);

 

    }

    echo '</table>';

    echo "<p><strong>Grand total: </strong>\$$total</p>";

}

?>
Link to comment
https://forums.phpfreaks.com/topic/276531-removing-item-from-shopping-cart/
Share on other sites

Sorry about that,

 

 

 

 

<?php

$cart_row = array(    'item' => $item,
    'unitprice' => $price,
    'quantity' => $quantity
);


if (!isset($_SESSION['cart']))
    $_SESSION['cart'] = array();


$_SESSION['cart'][] = $cart_row;


var_dump($_SESSION);
?>

 

 

 
 

 

<?php


if (!isset($_SESSION['cart']) || (count($_SESSION['cart']) == 0)) {
    echo '<p>Your cart is empty.</p>';
} else {
    echo '<table border="1">
    <tr><th>Item</th><th>Unit price</th><th>No. of units</th><th>Subtotal</th></tr>';
    $total = 0;
    
    foreach($_SESSION['cart'] as $item) {
    
        echo "<tr><td>{$item['item']} </td>
       
<td>\${$item['unitprice']}</td><td>{$item['quantity']}</td>
        <td>$".($item['unitprice'] * $item['quantity'])."</td>
<td>. $items[$item] . ' (<a href="' . $_SERVER['PHP_SELF'] . '?remove=' . $item . '">Remove</a>)        </td>


</tr>";
        $total += ($item['unitprice'] * $item['quantity']);


    }
    echo '</table>';
    echo "<p><strong>Grand total: </strong>\$$total</p>";
}




?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.