PHP Shopping Cart - Checkout Problem
Posted: Tue Mar 22, 2011 3:03 pm
Hi I have a bit of a problem, I have followed a tutorial to create a shoping cart. This has been implemented sucessfully so far. The actual problem is implementing the "Checkout" process itself. I haven;t got a clue on how to progress from here. My idea is to retrieve all the data stored within the session array and split it into its own variables so I can use $GET and INSERT into the database? Any pointers or help guys? Many Thanks... code below
Code: Select all
<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="carty.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
var_dump ($contents);
print "\n\n\n\n\n\n\n\n";
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="carty.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM Proj_Event WHERE Event_ID = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="carty.php?action=delete&Event_ID='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$Event_Title.' held at '.$Event_Location.'</td>';
$output[] = '<td>£'.$Event_Price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>£'.($Event_Price * $qty).'</td>';
$total += $Event_Price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
$output[] = '<a href="./order.php">Proceed to Basket and finalize order</a>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
//var_dump($output);
function showSubCart() {
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="carty.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM Proj_Event WHERE Event_ID = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td>'.$qty.' x </td>';
$output[] = '<td>'.$Event_Title.' at '.$Event_Location.'</td>';
$output[] = '<td>£'.$Event_Price.' = </td>';
$output[] = '<td>£'.($Event_Price * $qty).'</td>';
$total += $Event_Price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Total: <strong>£'.$total.'</strong></p>';
$output[] = '</form>';
$output[] = '<a href="./order.php">Proceed to Basket and finalize order</a>'; //---------------------- This is where I would like the checkout to occur
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
?>