473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
4 3735
..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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Daniel Hansen | last post by:
I am getting a "Fatal error: Call to undefined function: imagecreatefromjpeg() in..." error in one of my scripts, and after doing a bit of searching on the 'net I found various messages relating to...
3
by: PeterF | last post by:
Hello, what is wrong here? the purpose is to create an array of objects and then interate over it, calling some method from all of them. I get just the following at the marked line ("// FATAL...
4
by: gc | last post by:
I'm a PHP and MySQL newbie. I have a feeling a lot of you may have seen this before. I'm teaching myself PHP/MySQL and trying to setup a guestbook. I'm running latest versions of Apache, PHP and...
8
by: Tim Tyler | last post by:
I'm getting fatal errors when executing code - and my error handler is failing to trap them - so I get no stack backtrace :-( The error I am getting is: "Fatal error: Call to a member function...
2
by: dmitry.freitor | last post by:
Why would someone call a non-static provate member function from another non-static member function of the same class via the this pointer? Thanks. DF
7
by: Ook | last post by:
What am I doing wrong? This code gives a compile error: 'SortedList<T>::insert' : illegal call of non-static member function. I've tried several variations of this, but keep getting the same error....
4
by: Jesper Stocholm | last post by:
I have a recursive function that I would like to do a lot of recursive calls (preferebly 2^20 in total) The function is called as (with maxi = e.g. 100000) DoRecursion(0,maxi,bseed,sha); ...
7
by: inkexit | last post by:
I'm getting these two error mesages when I try to compile the below source code: error C2065: 'input_file' : undeclared identifier error C2228: left of '.eof' must have class/struct/union type ...
12
by: mast2as | last post by:
Hi everyone... I have a TExceptionHandler class that is uses in the code to thow exceptions. Whenever an exception is thrown the TExceptionHander constructor takes an error code (int) as an...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.