Page 1 of 1

Fatal error: Call to a member function on a non-object in

Posted: Mon Feb 02, 2004 9:58 pm
by stunhcterts
I have received this error message
Fatal error: Call to a member function on a non-object in update_cart.php on line 14

Code: Select all

<?php
<?
	if(!isset($_POST)) {
			
		header('Location: ../index.php');
	}

	else {
	
	
		foreach($_POST[quant] as $i=>$data) {
			if($_POST[quant] = "") {
				$_POST[quant] = "0";
			}
	$SESSION["cart"]->set_quant('$_POST[ID][$i]','$_POST[quant]');	
		}

		header('Location: ../index.php?page=shopping_cart');
	
	}
?>

?>
The code looks to be alll in order
line 14 is

Code: Select all

<?php
$SESSION["cart"]->set_quant('$_POST[ID][$i]','$_POST[quant]');	
?>
What member function should be defined?

My Friend told me he had defined the quant member func what do i do?

I am postign this for a friend
Please let me know if u need any other questions
There were no other relevant posts that helped me.
Thanks

Posted: Mon Feb 02, 2004 10:04 pm
by markl999
$SESSION["cart"]
$_SESSION["cart"] maybe ?

Posted: Tue Feb 03, 2004 5:46 am
by DuFF
Maybe he's just missing the underscore? Try changing $SESSION["cart"] to $_SESSION["cart"].

Posted: Tue Feb 03, 2004 9:49 am
by stunhcterts
we didnt need to put the underscore in since it was defined with a underscore

ok we fixed the one error by placing this in code

Code: Select all

<?php


require('../cart/cart.php');
require('../db/dbconn.php');

//start up sessions and cart if its not started already
	session_start();
	session_register("SESSION");

if (! isset($SESSION)){
	$SESSION = array();
}

if (! isset($SESSION["cart"])){
	$SESSION["cart"] = new Cart;
}


	if(!isset($_POST)) {
			
		header('Location: ../index.php');
	}

	else {
	
	
		foreach($_POST[quant] as $i=>$data) {
			if($_POST[quant] = "") {
				$_POST[quant] = "0";
			}
	$SESSION["cart"]->set_quant($_POST[ID][$i],$_POST[quant]);	
		}

		header('Location: ../index.php?page=shopping_cart');
	
	}


?>



Now it brings up this problem
The update_cart page is now not receiveing the data form the shopping_cart variables.


The code for the shopping_cart



Code: Select all

<?php
<?
if($SESSION["cart"]->count_items() > 0) {
?>
<form name='cart' method='post' action='cart/update_cart.php'>

<h1>Shopping Cart</h1><br>
<table align='left' width='100%' border='0' cellspacing='0' cellpadding='4'>
    
    <tr valign='top' bgcolor='#006633'> 
      <td align='center'><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>&nbsp;</font></td>
      <td> <div align='left'><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'><strong>Item</strong></font></div></td>
      <td align='center'><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>&nbsp;<strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>Quantity</font></strong></font></td>
      <td align='right'><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'><strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>Price</font></strong>&nbsp;</font></td>
      <td align='center'><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>&nbsp;<strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>Shipping</font></strong></font></td>
      <td align='center'><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'><strong>Subtotal</strong></font></td>
      <td align='center'><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'><strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>Delete</font></strong>&nbsp;</font></td>
    </tr>
<?


$qid = $SESSION["cart"]->product_list();

$total = 0;

foreach($qid as $i=>$data){

$query2 = db_query("SELECT * FROM inventory_master WHERE ID='$data'");
$cid = db_fetch_assoc($query2);
$quant = $SESSION["cart"]->quants[$i];
$link = "index.php?page=display&category=".$cid[category]."#".$data;
$price = $quant * $cid[price];
	
	if($quant > 100) {
		$shipping = $cid[shipping] * $quant * (3/4);
	}
	else if($quant > 50) {
		$shipping = $cid[shipping] * $quant * (3/5);
	}
	else if($quant > 10) {
		$shipping = $cid[shipping] * $quant * (1/3);
	}
	else {
		$shipping = $cid[shipping] * $quant;
	}

$subtotal = $price + $cid[shipping];
	
	$shipping = round($shipping,2);	
	$price = round($price, 2);

	$page = "<tr valign='top'> 
      		<td width='11%' align='center' bgcolor='#669966'> <font face='Arial, Helvetica, sans-serif'><img 				src='$cid[image]' 		name='image' width='30' height='30'> 
    		    </font></td>
    		  <td width='36%' bgcolor='#669966'><font face='Arial, Helvetica, sans-serif'><a 		href='$link'><strong>$cid[name]</strong></a></font></td>
  		    <td width='11%' align='center' bgcolor='#669966'><font face='Arial, Helvetica, sans-serif'> 
  		      <input name='quant[$i]' type='text' size='5' maxlength='4' value='$quant'>
  		      </font></td>
   		   <td width='9%' align='right' bgcolor='#669966'><font face='Arial, Helvetica, 		sans-serif'><strong>$$price</strong></font></td>
    		  <td width='12%' align='center' bgcolor='#669966'><strong><font face='Arial, Helvetica, 		sans-serif'>$$shipping</font></strong></td>
    		  <td width='11%' align='center' bgcolor='#669966'><strong><font face='Arial, Helvetica, 		sans-serif'>$$subtotal</font></strong></td>
   		   <td width='10%' align='center' bgcolor='#669966'> <font face='Arial, Helvetica, sans-serif'> 
  		      <input type='checkbox' name='delete[$i]'>
 		       <input type='hidden' name='ID[$i]' value='$cid[ID]'>
			</font></td>
 		   </tr>";	

	//if($quant != "0") {	
   		echo ($page);
	$total += $subtotal;
	//}

}
?>
    <tr bgcolor='#669966'><td colspan='7' align='right'><input type='submit' name='submit' value='Update Cart'></form>
    <tr valign='middle' bgcolor='#006633'> 
      <td colspan='2' align='right'><strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'> 
        </font></strong></td>
      <td width='11%' align='center'><strong></strong></td>
      <td width='9%' align='right'><strong></strong></td>
      <td width='12%' align='center'><strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>Total:</font></strong></td>
      <td width='11%' align='center'><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'><strong>$<?=$total?></strong></font></td>
      <td width='10%' align='center'> <strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'> 
        <form name='purchase' method='post' action='buy.php'><input type='submit' name='Submit22' value='Buy'>
        </font></strong></td>
    </tr>
  </table>
</form>
<?
}
else {
?>
There are currently no items in your cart.<br>Click <a href="index.php">here</a> to continue shopping.
<br> </br>
<br> </br>
<br> </br>
<br> </br>
<br> </br>
<br> </br>
<br> </br>
<br> </br>

<?
}


?>







?>

Posted: Tue Feb 03, 2004 11:34 am
by Weirdan
where is the Cart class defined?

Posted: Tue Feb 03, 2004 1:42 pm
by stunhcterts
the cart is defined in the cart.php

The code for the cart.php is as follows

Code: Select all

<?php
<?
//shopping cart
	

class Cart {

	var $items;
	var $quants;
	var $total_price;
	
	function Cart() {
	//Constructor
	$this->init();
	}
	
	function init() {
	// used to initialize and reset cart
	$this->items = array();
	$this->quants = array();
	$this->total_price = 0;
	}
	
	
	function get_quant(&$i) {
	// use to get quant from the parrallel array
		return $quants[i];
	}	

	function add_item(&$productid, $quant) {
	// used to add item(s) to the cart
		if(isset($productid)) {
			//setdefault($this->items[$productid], 0);

			$this->items[] = $productid;
			$this->quants[] = $quant;
		}
	}
	
	function setdefault(&$var, $default="") {
	/* if $var is undefined, set it to $default.  otherwise leave it alone */

		if (! isset($var)) {
			$var = $default;
		}
	}
	

	function set_quant(&$productid, $quant) {
	// set a product to a certain ammount
		if(isset($productid)) {
			if($quant = "") $quant = 0;
			foreach($this->items as $i=>$data) {
				if($productid = $data) $this->quants[$i] = $quant;
			}
		}
	}
	
	function remove_item(&$productid) {
	// removes a product from the cart
		if(isset($productid)) {
			unset($this->items[$productid]);
		}
	}

	function cleanup_cart() {
	// removes items with a quantity less then 1
		foreach($this->items as $productid => $quant) {
			if($quant < 1) {
				unset($this->items[$productid]);
			}
		}
	}
	
	function count_items() {
	// count the total number of items in cart
		$count = 0;
		foreach ($this->items as $productid => $quant) {
			$count += $quant;
		}
		
		return $count;
	}

	function product_list() {
	// return list of product seperated by commas
	
		$productid_list = array();
		foreach($this->items as $productid => $quant) {
			$productid_list[] = $quant;
			
		}
		return $productid_list;
	}

	function calc_total() {
	// caclulate the total
		$this->total = 0;

		$in = $this->product_list();
		if(empty($in)) {
			return;
		}
		
		$qid = db_query("SELECT ID, price FROM inventory_master WHERE ID IN ($in)");
		while($product = db_fetch_object($qid)) {
			$this->total += $this->items[$product->id] * $product->price;
		}
	}
	

}
?>

?>

Posted: Tue Feb 03, 2004 6:48 pm
by Weirdan
Include_once cart.php in shopping_cart.php and see if it works. Or, even better I think, in index.php