Forum Moderators: coopster
<?php
include ('functions.php');
session_start();
@ $new = $_GET['new'];
if($new)
{
//new item selected
if(!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
$_SESSION['items'] = 0;
$_SESSION['total_price'] ='0.00';
}
if(isset($_SESSION['cart'][$new]))
$_SESSION['cart'][$new]++;
else
$_SESSION['cart'][$new] = 1;
$_SESSION['total_price'] = calculate_price($_SESSION['cart']);
$_SESSION['items'] = calculate_items($_SESSION['cart']);
}
if(isset($_POST['save']))
{
foreach ($_SESSION['cart'] as $prodcode => $qty)
{
if($_POST[$prodcode]=='0')
unset($_SESSION['cart'][$prodcode]);
else
$_SESSION['cart'][$prodcode] = $_POST[$prodcode];
}
$_SESSION['total_price'] = calculate_price($_SESSION['cart']);
$_SESSION['items'] = calculate_items($_SESSION['cart']);
}
if($_SESSION['cart']&&array_count_values($_SESSION['cart']))
display_cart($_SESSION['cart']);
else
{
echo '<p>There are no items in your cart</p>';
}
$target = 'index.php';
// if we have just added an item to the cart, continue shopping in that category
if($new)
{
$details = get_product_details($new);
if($details['category_id'])
$target = 'index.php?page=show_category&category_id='.$details['category_id'].'&sort='.$_GET['sort'];
}
?>
function display_cart($cart, $change = true, $images = 1)
{
// display items in shopping cart
// optionally allow changes (true or false)
// optionally include images (1 - yes, 0 - no)
echo '<table style="margin-top: 20px;" border = "0" width = "95%" cellspacing = 0>
<form action = "index.php?page=show_cart" method = "post">
<tr><th colspan = '. (1+$images) .' bgcolor="#905f36">Item</th>
<th bgcolor="#905f36">Finish</th><th bgcolor="#905f36">Price</th><th bgcolor="#905f36">Quantity</th>
<th bgcolor="#905f36">Total</th></tr>';
//display each item as a table row
foreach ($cart as $prodcode => $qty)
{
$item = get_product_details($prodcode);
echo '<tr>';
if($images ==true)
{
echo '<td align = left>';
if (file_exists('images/products/small/'.$item['sm_image']))
{
$size = GetImageSize('images/products/small/'.$item['sm_image']);
if($size[0]>0 && $size[1]>0)
{
echo '<img src="images/products/small/'.$item['sm_image'].'" ';
echo 'width ="'. $size[0]/3 .'" height = "' .$size[1]/3 . '" />';
}
}
else
echo ' ';
echo '</td>';
}
echo '<td align = "left">';
echo '<a href = "index.php?page=show_product&product='.$prodcode.'">'.$item['description'].'</a>';
echo '</td>';
[b]
echo '<td></td>';
[/b]
echo '<td align = "center">$'.number_format($item['price'], 2);
echo '</td><td align = "center">';
// if we allow changes, quantities are in text boxes
if ($change == true)
echo "<input type = 'text' name = \"$prodcode\" value = \"$qty\" size = 3>";
else
echo $qty;
echo '</td><td align = "center">$'.number_format($item['price']*$qty,2)."</td></tr>\n";
}
// display total row
echo "<tr>
<th colspan = ". (3+$images) ." bgcolor=\"#905f36\"> </td>
<th align = \"center\" bgcolor=\"#905f36\">
".$_SESSION['items']."
</th>
<th align = \"center\" bgcolor=\"#905f36\">
\$".number_format($_SESSION['total_price'], 2).
'</th>
</tr>';
// display save change button
if($change == true)
{
echo '<tr>
<td colspan = '. (2+$images) .'> </td>
<td align = "center">
<input type = "hidden" name = "save" value = true>
<input type = "image" src = "images/save.png"
border = 0 alt = "Save Changes">
</td>
<td> </td>
</tr>';
}
echo '</form></table>';
}
?>