Fatal error: Call to a member function query() on a non-object
im getting an error on this line of code in red, can anyone lend a hand please
Code:
<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return '<p class="cartResponse">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 class="cartResponse">You have '.count($items).' item'.$s.' in your shopping cart</p>';
}
}
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[] = '<div id="formDiv"><form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table width="100%" height="83" border="0" cellspacing="0" cellpadding="0" class="cartItems">';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM products WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = "<td width='80'>\n<a href='product.php?id=".$id."&prod=".$productID."&view=A '>";
$output[] ="<img class='barImg' src='assets/main_small/A_".$productID.".jpg' width='50' height='50' /></a>\n</td>\n";
$output[] = '<td valign="bottom" width="155">';
$output[] = "<ul class='cartDescription'>\n<li class='cartDescript1'>";
$output[] = strtoupper($itemName);
$output[] = "</li>\n<li class='cartDescript2'>";
$str6 = ($productID);
$output[] = strtoupper(substr($str6, 0, 6));
$output[] ="</li>\n</ul>\n";
$output[] = '</td>';
$output[] = "<td valign='bottom' width='160'>\n";
$output[] ="<ul class='cartDescription'>\n<li class='cartDescript1'>";
$output[] = strtoupper($fabricName);
$output[] = "</li>\n<li class='cartDescript2'>";
$str3 = ($rowX['productID']);
$three = strtoupper(substr($str3, 0, 3));
if ($three == "VOY") {
$str3 = $productID;
$output[] = strtoupper(substr($str3, -3));
}else {
$str3 = $productID;
$output[] = strtoupper(substr($str3, -2));
}
$output[] = "</li>\n</ul>";
$output[] = '<td valign="bottom" width="60" ><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td valign="bottom" width="80" >£'.($price * $qty).'</td>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p class="totalTxt">GRAND TOTAL</p>';
$output[] = '<p class="totalNum"><strong>£'.$total.'</strong></p>';
$output[] = '<div id="updateBtn"><button type="submit"><strong>UPDATE CART >></strong></button></div>';
$output[] = '<a href="cartCheckout.php "><div id="continueBtn">CONTINUE</div></a>';
$output[] = '</form></div>';
} else {
$output[] = '<p><!--You shopping cart is empty.--></p>';
}
return join('',$output);
}
?>