Eddietse91 Posted April 4, 2013 Share Posted April 4, 2013 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/276531-removing-item-from-shopping-cart/ Share on other sites More sharing options...
Love2c0de Posted April 4, 2013 Share Posted April 4, 2013 Can you format your code please? Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/276531-removing-item-from-shopping-cart/#findComment-1422918 Share on other sites More sharing options...
Eddietse91 Posted April 4, 2013 Author Share Posted April 4, 2013 (edited) 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 April 4, 2013 by Eddietse91 Quote Link to comment https://forums.phpfreaks.com/topic/276531-removing-item-from-shopping-cart/#findComment-1422937 Share on other sites More sharing options...
Eddietse91 Posted April 5, 2013 Author Share Posted April 5, 2013 Can anyone help me please? Quote Link to comment https://forums.phpfreaks.com/topic/276531-removing-item-from-shopping-cart/#findComment-1423197 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.