The cart boss! The CART!!
Posted: Fri Oct 13, 2006 1:44 am
I used this tutorial as a basis for my cart: http://www.thewatchmakerproject.com/jou ... pping-cart
It used an 'exploding strings' method which I found to be clunky at best. So, I changed it around to use an array in the session variable. I think this is superior but, I submit my code to you, the ultimate code gurus to pick apart... be gentle... but not too gentle... I'll never learn that way.
It uses two files: functions.php and cart.php.
functions.php:
cart.php:
So, if you can find it in your collectively infinite wisdom(s) to rip my code a new one, please do.
Thanks,
Tyler P
Jcart | Removed Mysql login credentials
It used an 'exploding strings' method which I found to be clunky at best. So, I changed it around to use an array in the session variable. I think this is superior but, I submit my code to you, the ultimate code gurus to pick apart... be gentle... but not too gentle... I'll never learn that way.
It uses two files: functions.php and cart.php.
functions.php:
Code: Select all
<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return '<center><b>0</b> items</center>';
} else {
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="cart.php">'.count($cart).' item'.$s.' in your shopping cart</a></p>';
}
}
function showCart() {
$cart = $_SESSION['cart'];
if ($cart) {
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($cart as $id=>$qty) {
$Category = substr($id, 0, 1);
$ProductID = substr_replace($id,'',0,1);
$dbh=mysql_connect ("localhost", "***", "***") or die ('I cannot connect to the database, try again later.');
mysql_select_db ('guttersg_ggoods');
switch ($Category) {
case 'c':
$query = "SELECT * FROM CLOTHING WHERE ProductID = '".$ProductID."'";
break;
case 'i':
$query = "SELECT * FROM INCENSE WHERE ProductID = '".$ProductID."'";
break;
}
$result = mysql_query($query) or die('Query failed.');
$row = mysql_fetch_assoc($result);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$row['Name'].'</td>';
$output[] = '<td>$'.$row['Price'].'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>$'.($row['Price'] * $qty).'</td>';
$total += $row['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>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
?>Code: Select all
<?php
session_start();
require_once('includes/db.php');
require_once('includes/global.php');
require_once('includes/functions.php');
$cart = $_SESSION['cart'];
$action = $_GET['action'];
$id = $_GET['id'];
switch ($action) {
case 'add':
if ($cart) {
if ($cart[$id]) {
$cart[$id] += $_POST['quantity'];
} else {
$cart[$id] = $_POST['quantity'];
}
} else {
$cart = array();
$cart[$id] = $_POST['quantity'];
}
break;
case 'delete':
if ($cart) {
unset($cart[$id]);
}
break;
case 'update':
if ($cart) {
foreach ($_POST as $key=>$value) {
if (stristr($key,'qty')) {
$id = str_replace('qty','',$key);
$cart[$id] = $value;
}
}
}
break;
}
$_SESSION['cart'] = $cart;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Gutter's Goods Checkout</title>
<link rel="stylesheet" href="css/styles.css" />
</head>
<body>
<div id="shoppingcart">
<h1>Your Shopping Cart</h1>
<?php
echo writeShoppingCart();
?>
</div>
<div id="contents">
<h1>Please update quantities and click Submit to continue.</h1>
<?php
echo showCart();
?>
<p><a href="index.php">Continue Shopping</a></p>
</div>
</body>
</html>Thanks,
Tyler P
Jcart | Removed Mysql login credentials