You are here: Re: Fatal error: Call to a member function add_item() on a non-object « PHP Programming Language « IT news, forums, messages
Re: Fatal error: Call to a member function add_item() on a non-object

Posted by Flic on 02/02/07 16:41

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>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация