Hi guys
I'm basically trying to implement this cart into my pages:
http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cartI understand it for the mostpart (I'm a novice at PHP but can generally figure out what's going on here).
When I run this code on my cart display page I get the following error:
<?php
function showCart() {
$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_add.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM Items WHERE ItemID = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart_add.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$ItemName.' by '.$author.'</td>';
$output[] = '<td>£'.$UnitPrice.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>£'.($UnitPrice * $qty).'</td>';
$total += $UnitPrice * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: £'.$total.'</p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
showCart();
?>Notice: Undefined variable: db in C:\wamp\www\Adept\cart_add.php on line 88
Fatal error: Call to a member function query() on a non-object in C:\wamp\www\Adept\cart_add.php on line 88Line 88 is pointing to this line of code
$result = $db->query($sql);I realise that $db isn't defined....but should it be? I think I'm missing something here. I looked through all of the poster's code and nowhere does he define that variable....maybe the function isn't returning anything?
Thanks for any help guys.