Adding item to cart from array, data not carried over

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Flic
Forum Newbie
Posts: 1
Joined: Tue Feb 06, 2007 8:24 am

Adding item to cart from array, data not carried over

Post by Flic »

Hi, I have some shopping basket code which works in that the item gets added to the shopping basket, but the information (name, price etc) isn't carried over.

I've only recently started learning php so am not sure why it doesn't work properly and would really appreciate it if someone could take a look and let me know where I am going wrong. It did work before but I heavily modified it for my needs hence it not working now . :?

Code that its being called from:

Code: Select all

<?php
$products = array();
$products[1] = array("id"=>123,"name"=>"Silo Venting Filter","price"=>123.45,"sub"=>"venting_system", "desc"=>"A cylindrically shaped dust collector for venting of pneumatically filled silos, the stainless steel body contains vertically mounted filter elements. The air jet cleaning system is integrated in the hinged weather protection cover.");

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>";
}
?>
The cart page:

Code: Select all

<?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

}
?>

<html links>

<?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>


Any pointers and help would be much apprciated!

Thanks!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

$cart->add_item($product['id'],$product['price'],$product['name']);
...does not match method prototype...

Code: Select all

function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE)
Make sure you are developing with...

Code: Select all

ini_set('display_errors', true);
error_reporting(E_ALL | E_STRICT);
...set. PHP would probably have told you that. Turn display_errors off for production when you are confident everything is working.
Post Reply