By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,053 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,053 IT Pros & Developers. It's quick & easy.

Fatal error: Call to a member function add_item() on a non-object

P: n/a
Hi, I am a relative beginner with php and have run into the problem in
the subject line. I wonder if anyone could point out what the problem
is as I have tried a few different things and it still doesn't like
it.

The code itself is:

if($_POST['add']) {
$product = $products[$_POST['id']];
$cart->add_item($product['id'],$product['price'],$product['name']);
}

with the add_item line being the problem.

Just in case, it is being called using the following code:

<?php
$products = array();
$products[1] = array("id"=>123,"name"=>"Silo Venting
Filter","price"=>123.45,"sub"=>"venting_system", "desc"=>"temp");

foreach($products as $p) {
echo "<div class='acc'>";
echo "<form method='post' action='cart.php'>";
echo "<input type='hidden' name='id' value='".$p['id']."'/>";
echo "<img src='images/".$p['sub'].".jpg' alt='".$p['name']."'>";
echo "<div><h1><a class='silolink' href='accessories/".
$p['sub'].".html'>".$p['name']."</a></h1>";
echo $p['desc'];
echo "<br><br>".$p['price'];
echo "<input type='submit' value='Add to cart' name='add'></form>";
echo "</div></div>";
}
?>

I would much appreciate it if someone could help! I learn from coding
and mistakes!

Thanks

Feb 2 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
..oO(Flic)
>Hi, I am a relative beginner with php and have run into the problem in
the subject line. I wonder if anyone could point out what the problem
is as I have tried a few different things and it still doesn't like
it.

The code itself is:

if($_POST['add']) {
$product = $products[$_POST['id']];
$cart->add_item($product['id'],$product['price'],$product['name']);
}

with the add_item line being the problem.
The error message is pretty clear. Where is $cart coming from?

Micha
Feb 2 '07 #2

P: n/a
On 2 Feb, 15:30, Michael Fesser <neti...@gmx.dewrote:
.oO(Flic)
Hi, I am a relative beginner with php and have run into the problem in
the subject line. I wonder if anyone could point out what the problem
is as I have tried a few different things and it still doesn't like
it.
The code itself is:
if($_POST['add']) {
$product = $products[$_POST['id']];
$cart->add_item($product['id'],$product['price'],$product['name']);
}
with the add_item line being the problem.

The error message is pretty clear. Where is $cart coming from?

Micha
Ah, I checked and it was in the wrong file, the code I started with
had everything on the same page and that bit was seperate from the
rest of it so got missed on the move to two pages. As I said I've only
just recently started this so it wasn't clear to me, I was looking at
the actual in the add_item brackets.

So thats that sorted, but its still not working properly. It adds an
item but not the information for it, can anyone point out why?

A bit more code:

function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE)
{ // adds an item to cart
if($this->items[$itemid] 0)
{ // the item is already in the cart..
// so we'll just increase the quantity
$this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid];
$this->_update_total();
} else {
$this->items[]=$itemid;
$this->itemqtys[$itemid] = $qty;
$this->itemprices[$itemid] = $price;
$this->iteminfo[$itemid] = $info;
}
$this->_update_total();
} // end of add_item

Once again any help appreciated! Thanks

Feb 2 '07 #3

P: n/a
On 2 Feb, 16:17, "Flic" <Felici...@gmail.comwrote:
On 2 Feb, 15:30, Michael Fesser <neti...@gmx.dewrote:


.oO(Flic)
>Hi, I am a relative beginner with php and have run into the problem in
>the subject line. I wonder if anyone could point out what the problem
>is as I have tried a few different things and it still doesn't like
>it.
>The code itself is:
>if($_POST['add']) {
$product = $products[$_POST['id']];
$cart->add_item($product['id'],$product['price'],$product['name']);
>}
>with the add_item line being the problem.
The error message is pretty clear. Where is $cart coming from?
Micha

Ah, I checked and it was in the wrong file, the code I started with
had everything on the same page and that bit was seperate from the
rest of it so got missed on the move to two pages. As I said I've only
just recently started this so it wasn't clear to me, I was looking at
the actual in the add_item brackets.

So thats that sorted, but its still not working properly. It adds an
item but not the information for it, can anyone point out why?

A bit more code:

function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE)
{ // adds an item to cart
if($this->items[$itemid] 0)
{ // the item is already in the cart..
// so we'll just increase the quantity
$this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid];
$this->_update_total();
} else {
$this->items[]=$itemid;
$this->itemqtys[$itemid] = $qty;
$this->itemprices[$itemid] = $price;
$this->iteminfo[$itemid] = $info;
}
$this->_update_total();
} // end of add_item

Once again any help appreciated! Thanks- Hide quoted text -

- Show quoted text -
items[$itemid] is the element of items whose index it $itemid
However, items[]=$itemid simply adds one more element to the array
items and gives it a value of $itemid.
So the Then and Else parts are expecting differently structured
arrays.

Feb 2 '07 #4

P: n/a
Here is the whole code so you can get a better overview and perhaps
spot something I haven't. Would have linked to a page but of course
that wouldn't have worked as its php! I've already given the code
thats calling the cart and ther est of the code on that page is normal
html. So here is the whole cart.

<?php
session_start();
$cart =& $_SESSION['cart']; // point $cart to session cart.
if(!is_object($cart)) $cart = new siloCart(); // if $cart
( $_SESSION['cart'] ) isn't an object, make a new cart

class siloCart {
var $total = 0;
var $itemcount = 0;
var $items = array();
var $itemprices = array();
var $itemqtys = array();
var $iteminfo = array();

function cart() {} // constructor function

function get_contents()
{ // gets cart contents
$items = array();
foreach($this->items as $tmp_item)
{
$item = FALSE;

$item['id'] = $tmp_item;
$item['name'] = $this->itemname[$tmp_item];
$item['price'] = $this->itemprices[$tmp_item];
$item['sub'] = $this->itemsub[$tmp_item];
$item['desc'] = $this->itemdesc[$tmp_item];
$item['subtotal'] = $item['qty'] * $item['price'];
$items[] = $item;
}
return $items;
} // end of get_contents
function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE)
{ // adds an item to cart
if($this->items[$itemid] 0)
{ // the item is already in the cart..
// so we'll just increase the quantity
$this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid];
$this->_update_total();
} else {
$this->items[]=$itemid;
$this->itemqtys[$itemid] = $qty;
$this->itemprices[$itemid] = $price;
$this->iteminfo[$itemid] = $info;
}
$this->_update_total();
} // end of add_item
function edit_item($itemid,$qty)
{ // changes an items quantity

if($qty < 1) {
$this->del_item($itemid);
} else {
$this->itemqtys[$itemid] = $qty;
}
$this->_update_total();
} // end of edit_item
function del_item($itemid)
{ // removes an item from cart
$ti = array();
$this->itemqtys[$itemid] = 0;
foreach($this->items as $item)
{
if($item != $itemid)
{
$ti[] = $item;
}
}
$this->items = $ti;
$this->_update_total();
} //end of del_item
function empty_cart()
{ // empties / resets the cart
$this->total = 0;
$this->itemcount = 0;
$this->items = array();
$this->itemprices = array();
$this->itemqtys = array();
$this->itemdescs = array();
} // end of empty cart
function _update_total()
{ // internal function to update the total in the cart
$this->itemcount = 0;
$this->total = 0;
if(sizeof($this->items 0))
{
foreach($this->items as $item) {
$this->total = $this->total + ($this-
>itemprices[$item] * $this->itemqtys[$item]);
$this->itemcount++;
}
}
} // end of update_total

}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="code/silosuk.css">
<link rel="stylesheet" type="text/css" href="code/silosuk_silos.css">
<link rel="stylesheet" type="text/css" href="code/silosuk_form.css">
</head><body>
<div id="logo"></div><div id="header"><div id="linkcontainer">
<a href="cart.php" class="links">Cart</a>
<a href="contact.html" class="links">Contact</a>
<a href="accessories.php" class="links" style="width:
18%;">Accessories</a>
<a href="silos.html" class="links">Silos</a>
<a href="index.html" class="links">About</a>
</div></div><div id="content"><div id="cart">

<?php

if($_POST['add']) {
$product = $products[$_POST['id']];
$cart->add_item($product['id'],$product['price'],$product['name']);
}
if($_POST['remove']) {
$rid = intval($_POST['id']);
$cart->del_item($rid);
}

echo "<table><tr><td>ID</td>";
echo "<td>Name</td>";
echo "<td>Price</td>";
echo "<td>Quan</td>";
echo "<td>Subtotal</td></tr>";

if($cart->itemcount 0) {
foreach($cart->get_contents() as $item) {
echo "<tr><td>".$item['id']."</td>";
echo "<td>".$item['info']."</td>";
echo "<td>".number_format($item['price'],2)."</td>";
echo "<td>".$item['qty']."</td>";
echo "<td>".number_format($item['subtotal'],2)."</
td>";
echo "<td><form method=post><input type='hidden'
name='id' value='".$item['id']."'/><input type='submit' name='remove'
value='X'/></form></td></tr>";
}
echo "<tr><td colspan=4>Sub total:</td><td>".number_format($cart-
>total,2)."</td></tr>";
echo "<tr><td colspan=4>VAT:</td><td>".number_format($cart->total,
2)."</td></tr>";
echo "<tr><td colspan=4>Total:</td><td>".number_format($cart->total,
2)."</td></tr>";
echo "</table>";
} else {
echo "<tr><td colspan=5>- No items found in cart -</
td></tr>";
echo "</table>";
}

?>
</div></div></body>
</html>

Feb 2 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.