Jump to content

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>";
}




?>

 

 

Edited by Eddietse91
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.