I am learning some PHP and I am trying to create a all in one page shopping cart.
I have read into and can see the benefits of a SQL bases system, but I want to learn the basics first. In doing so I have create a all-in-one page that contains the products in an associated array, as well as a form that submits to itself.
What I want to achieve is:
- A product can only be purchased once (buy button replaced with a remove button)
- The item and its cost are added to the cart below
- A user can either remove it from the cart, or the item list
- The total cost should be updated as required.
- The "checkout" button will submit item name and cost
- The form posts to itself and does not require any SQL
My current problem is:
- I cannot purchase more than one item at a time, i.e. the cart only contains the last purchased item
- I cannot get it to "check" if an item has been purchased and if so, replace the "buy" with "remove"
- I cannot display the item details in the cart
- The checkout button does not pass any details to my test
Again, I am not looking for a SQL solution yet, just a pure PHP using $_SESSION
and $_POST
and would like to use buttons instead of <a href add?>
type links.
Thanks for the lengthy read in advance here is the code:
<?php
session_start ();
$items = array (
'A123' => array (
'name' => 'Item1',
'desc' => 'Item 1 description...',
'price' => 1000
),
'B456' => array (
'name' => 'Item40',
'desc' => 'Item40 description...',
'price' => 2500
),
'Z999' => array (
'name' => 'Item999',
'desc' => 'Item999 description...',
'price' => 9999
)
);
if (! isset ( $_SESSION ['cart'] )) {
$_SESSION ['cart'] = array ();
}
// Add
if (isset ( $_POST ["buy"] )) {
$_SESSION ['cart'] = $_POST;
}
// Delete Item
else if (isset ( $_POST ['delete'] )) { // a remove button has been clicked
unset ( $_POST ['delete'] ); //
}
// Empty Cart
else if (isset ( $_POST ["delete"] )) { // remove item from cart
unset ( $_SESSION ['cart'] );
}
?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<?php
foreach ( $items as $ino => $item ) {
$title = $item ['name'];
$desc = $item ['desc'];
$price = $item ['price'];
echo " <p>$title</p>";
echo " <p>$desc</p>";
echo "<p>\$$price</p>";
if ($_SESSION ['cart'] == $ino) {
echo '<img src="carticon.png">';
echo "<p><button type='submit' name='delete' value='$ino'>Remove</button></p>";
} else {
echo "<button type='submit' name='buy' value='$ino'>Buy</button> ";
}
}
?>
</form>
<?php
if (isset ( $_SESSION ["cart"] )) {
?>
<form action='(omitted link)'
target='_blank' method='post'
enctype='application/x-www-form-urlencoded'>
<table>
<tr>
<th>Product</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php
$total = 0;
foreach ( $_SESSION ["cart"] as $i ) {
?>
<tr>
<td>
<?php echo($_SESSION["cart"]); ?> <!--Item name-->
</td>
<td>price<?php echo($_SESSION["price"][$i] ); ?>
<!--Item cost-->
</td>
<td><button type='submit' name='delete' value='$ino'>Remove</button>
</p></td>
</tr>
<?php
$total = + $_SESSION ["amounts"] [$i];
}
$_SESSION ["total"] = $total;
?>
<tr>
<td colspan="2">Total: $<?php echo($total); ?></td>
<td><input type='submit' value='Checkout' /></td>
</tr>
<tr>
<td><button type='submit' name='clear'>Clear cart</button></td>
</tr>
</table>
</form>
<?php } ?>