Author Topic: Help with my cart  (Read 823 times)

0 Members and 1 Guest are viewing this topic.

Offline RynManTopic starter

  • Irregular
  • Posts: 49
    • View Profile
Help with my cart
« on: June 12, 2010, 03:39:15 AM »
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:

Code: [Select]
<?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;
}
$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>&pound;'.$UnitPrice.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>&pound;'.($UnitPrice $qty).'</td>';
$total += $UnitPrice $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: &pound;'.$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

Code: [Select]
$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.




Offline ignace

  • Guru
  • Freak!
  • *
  • Posts: 5,093
  • Gender: Male
    • View Profile
Re: Help with my cart
« Reply #1 on: June 12, 2010, 03:47:40 AM »
Quote
I realise that $db isn't defined....but should it be?

Quote
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?
Developer from Belgium, Vlaams-Brabant

Offline RynManTopic starter

  • Irregular
  • Posts: 49
    • View Profile
Re: Help with my cart
« Reply #2 on: June 12, 2010, 03:53:37 AM »
Quote
I realise that $db isn't defined....but should it be?

Quote
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). 


Offline RynManTopic starter

  • Irregular
  • Posts: 49
    • View Profile
Re: Help with my cart
« Reply #3 on: June 12, 2010, 03:55:43 AM »
That site seems to be down right now. 

The same tutorial is posted here:

http://www.studyblog.net/2008/11/a-simple-php-shopping-cart-script/

Offline ignace

  • Guru
  • Freak!
  • *
  • Posts: 5,093
  • Gender: Male
    • View Profile
Re: Help with my cart
« Reply #4 on: June 12, 2010, 04:15:37 AM »
Quote
(*Note*: I’m using a PHP class to handle my database connections, so your code may need to be slightly different).

It seems like you will need to adjust the code to make it work with the database.
« Last Edit: June 12, 2010, 04:16:08 AM by ignace »
Developer from Belgium, Vlaams-Brabant

Offline RynManTopic starter

  • Irregular
  • Posts: 49
    • View Profile
Re: Help with my cart
« Reply #5 on: June 12, 2010, 04:48:52 AM »
If someone else had any ideas id be extremely grateful.