Jump to content

Help with my cart


RynMan

Recommended Posts

Hi guys

 

I'm basically trying to implement this cart into my pages:

 

http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart

 

I 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 88

 

Line 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.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/204558-help-with-my-cart/
Share on other sites

I realise that $db isn't defined....but should it be?

 

Notice: Undefined variable: db in C:\wamp\www\Adept\cart_add.php  on line 88

 

What do you think? Where do you define $db - if anywhere?

 

Well....that's my question.  Is it defined somewhere that I'm missing?  I can't see it defined anywhere in my code, nor the code the poster on that page has written, however that means very little (as I said I was a PHP novice). 

 

 

Link to comment
https://forums.phpfreaks.com/topic/204558-help-with-my-cart/#findComment-1071072
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.