Hi,
I wonder if anyone can help me?
I have this script which puts together a basic shopping cart. The idea is, the main shop is on index.php, then the customer clicks an item they want and gets taken to the cart (cart.php). The items are stored in a database and are looked up by their id number. This is all done using sessions. All this is fine.
I added a form to cart.php so a form where customers enter shipping details is added. These details, along with the contents of the cart, need to be sent by email to the shop owner. This system does not need to deal with payments - these will be taken on delivery.
The email is sent in a file called sendorder.php. The functions to write and display the cart are in an include file called functions.
This is how the cart is written:
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM table1 WHERE Id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$Category.' : '.$Item.'</td>';
$output[] = '<td>£'.$Price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>£'.($Price * $qty).'</td>';
$total += $Price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>';
$output[] = '<div><button type="submit" action="" method="POST">Confirm Order</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>Your shopping cart is empty.</p>';
}
return join('',$output);
}
And this is my mail code:
<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global2.inc.php');
// Include functions
require_once('inc/functions2.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
//variables posted from form, eg $Firstname = $_POST etc
$Subtotal = ($Price * $qty);
$to = "myemail";
$headers = "FROM: $Firstname $Surname <$Email>";
$subject = "ORDER";
$cart = $items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM table1 WHERE Id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$Order =
"$Category $Item £$Price QTY: $qty Subtotal: $Subtotal
Grand Total: $total";
}
$body = "A new order has been submitted via your website:
ORDER:
//THIS IS WHERE I NEED THE ORDER TO DISPLAY
$Order
//Shipping info
//Customers details
mail($to, $subject, $body, $headers);
echo "Thank you.";
?>
The code above will display the first line of the order only, but this displays properly, including the item name, category and price. (Although it does not calculate the subtotal and consequently not the total - although this is the least of my problems!!) Using a slightly different variation of the above code, I can also get the cart to display the data in terms of 1,1,1,2,2,2,2 ie 3 of item 1 and 4 of item 2. I can post this is needed, along with any other code - obviosuly this is not it all!
Any help or pointers would be appreciated.
Thanks!