WibblyWobbly WibblyWobbly - 8 days ago 6
HTML Question

Add items to a cart from an array

I'm creating a php driven shopping cart as part of a task.
All products are currently stored in an array (I'm aware they would realistically be stored in a database, however for this task it isn't required)

I would like to know if its possible to add items to a cart from clicking a link, upon clicking proceed, it would take the user to a summary screen, displaying the cost of each item (value displayed from the array) and total cost of all items.

I am also looking to include a promotion code input box, is it possible to calculate and display a discount on button click, E.G "Update"

So far, I only have the products displayed in a table which is ok, the cart section is confusing me at the moment as well as the discount section.

<?php
$Item1 = array('SKU'=>test1, 'name'=>ProductTest1, 'Price'=>10.00);
$Item2 = array('SKU'=>test2, 'name'=>ProductTest2, 'Price'=>11.00);
$Item3 = array('SKU'=>test3, 'name'=>ProductTest3, 'Price'=>12.00);

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<div id="container">
<div id="main">
<table>
<tr>
<th>SKU</th>
<th>Name</th>
<th>Price</th>
<th>Action</th>
</tr>
<tr>
<td><?php echo $Item1[SKU]; ?></td>
<td><?php echo $Item1[name]; ?></td>
<td><?php echo '£'. number_format($Item1[Price],2); ?></td>
<td><a href=#>Add To Cart</a></td>
</tr>
<tr>
<td><?php echo $Item2[SKU]; ?></td>
<td><?php echo $Item2[name]; ?></td>
<td><?php echo '£'. number_format($Item2[Price],2); ?></td>
<td><a href=#>Add To Cart</a></td>
</tr>
<tr>
<td><?php echo $Item3[SKU]; ?></td>
<td><?php echo $Item3[name]; ?></td>
<td><?php echo '£'. number_format($Item3[Price],2); ?></td>
<td><a href=#>Add To Cart</a></td>
</tr>
</table>
<input id="Proceed" type="Submit" value="Proceed">
</div>
</div>
</body>
</html>

Answer

At first this is not good way to use the array like this. Try this:

<?php
$products = array(
    array('SKU'=>test1, 'name'=>ProductTest1, 'Price'=>10.00),
    array('SKU'=>test2, 'name'=>ProductTest2, 'Price'=>11.00),
    array('SKU'=>test3, 'name'=>ProductTest3, 'Price'=>12.00),
);

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<div id="container">
<div id="main">
<table>
<tr>
<th>SKU</th>
<th>Name</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php foreach ($products as $key => $product): ?>
<tr>
<td><?php echo $product['SKU']; ?></td>
<td><?php echo $product['name']; ?></td>
<td><?php echo '£'. number_format($product['Price'],2); ?></td>
<td><a href="#">Add To Cart</a></td>
</tr>
<?php endif; ?>
</table>
<input id="Proceed" type="Submit" value="Proceed">
</div>
</div>
</body>
</html>

Now how to solve the cart. You have to use something persistent. The session will be easiest:

<?php
session_start();
$products = array(
    array('SKU'=>test1, 'name'=>ProductTest1, 'Price'=>10.00),
    array('SKU'=>test2, 'name'=>ProductTest2, 'Price'=>11.00),
    array('SKU'=>test3, 'name'=>ProductTest3, 'Price'=>12.00),
);

if (isset($_GET['action'] && $_GET['action'] === 'addToCart') {
   if (!isset($_SESSION['cart']) $_SESSION['cart'] = array();
   $_SESSION['cart'][] = $_GET['product'];
}

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<div id="container">
<div id="main">
<?php if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])): ?>
<ul id="cart">
<?php foreach($_SESSION['cart'] as $product): ?>
   <li><?= $products[$product]['name'] ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

<table>
<tr>
<th>SKU</th>
<th>Name</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php foreach ($products as $key => $product): ?>
<tr>
<td><?php echo $product['SKU']; ?></td>
<td><?php echo $product['name']; ?></td>
<td><?php echo '£'. number_format($product['Price'],2); ?></td>
<td><a href="?action=addToCart&product=<?php echo $key ?>">Add To Cart</a></td>
</tr>
<?php endif; ?>
</table>
<input id="Proceed" type="Submit" value="Proceed">
</div>
</div>
</body>
</html>

It should work. Just adapting your code.

Comments