I am working on a simple order system, based on the sitepoint book “Build your own Database Driven Web Site Using PHP & MYSQL” By Kevin Yank.
the peice of code I am stuck on is the following:
if (isset($_GET['cart']))
{
$cart = array();
$total = 0;
foreach ($_SESSION['cart'] as $id)
{
foreach ($items as $product)
{
if ($product['id'] == $id)
{
$cart[] = $product;
$total += $product['price'];
break;
}
}
}
include 'cart.html.php';
exit();
}
To display this cart, the following script is used:
<body>
<h1>Your Shopping Cart</h1>
<?php if (count($cart) > 0): ?>
<table>
<thead>
<tr>
<th>Item Description</th>
<th>Price</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Total:</td>
<td>$<?php echo number_format($total, 2); ?></td>
</tr>
</tfoot>
<tbody>
<?php foreach ($cart as $item): ?>
<tr>
<td><?php htmlout($item['desc']); ?></td>
<td>
$<?php echo number_format($item['price'], 2); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>Your cart is empty!</p>
<?php endif; ?>
<form action="?" method="post">
<p>
<a href="?">Continue shopping</a> or
<input type="submit" name="action" value="Empty cart"/>
</p>
</form>
</body>
This is the code is build on a preset array. I am working with a table with a few columns in mysql.
I have am trying to adapt it so that the content is drawn out of a Mysql table. I have the following so far:
if (isset($_GET['order']))
{
$orders = array();
$total = 0;
foreach ($_SESSION['order'] as $id)
{
while($row = mysql_fetch_assoc($productsSql)) {
foreach ($prId as $product)
{
if ($row == $id) {
$orders[] = $product;
$total += $prPrice1;
break;
}
}
}
include($docRoot . '/orders-finalize.php');
exit();
}
}
To display this cart I have adapated the script like this:
$pageContent = '
<h2>Your order</h2>
';
if (count($order) > 0) {
$pageContent .= '
<table>
<thead>
<tr>
<th>Item Description</th>
<th>Price</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Total:</td>
<td>R'.number_format($total, 2).'</td>
</tr>
</tfoot>
<tbody>
';
foreach ($order as $item) {
$pageContent .= '
<tr>
<td>'.$item['desc'].'</td>
<td>
R'.number_format($item['price1'], 2).'
</td>
</tr>
';
}
$pageContent .= '
</tbody>
</table>
';
if (count($order) == 0) {
$pageContent .= '
<p>Your cart is empty!</p>
';
}
}
$pageContent .= '
<form action="?" method="post">
<p>
<a href="?">Continue shopping</a> or
<input type="submit" name="action" value="Empty cart"/>
</p>
</form>
';
echo $head1 . $pageDetails . $head2 . $header . $menu . $belowMenu . $content . $pageContent . $footer . $pageScripts;
exit;
All this seems to do is produce my cart in a fashion where when viewed it displays a list of only the items’ id, e.g. where the description and price are supposed to be, I only get the items id in both fields… I also get a total price of 0.
Can anyone spot where I am going wrong here?
Or atleast try to give me some input so that I get going in the right direction!
Thanks!!