Jump to content

my class works great in local, not works on server


saeed_violinist

Recommended Posts

Hi firends,

 

I have created a simple shopping cart class, it works fin in local (php 5) but not works at all in server (php4), although I tried to make it compatible with php 4 too.

 

I got this error ->

Fatal error: Call to a member function on a non-object in /home/phomes/domains/persianshome.com/public_html/shop/cart.php on line 34

 

class is :

<?php

# Shopping Cart Class

class CLS_CART {

# Member Variables
var $items;	// items in cart
var $total; // total cart price

# Cart Class Constructor
function CLS_CART() {
	$this->items = array(); // inititalize empty an array
	$this->total = 0;		// inititalize total price to zero
}

# Add Items To Cart
function CRT_AddItem($id, $name, $price, $qty = 1) {		
	// check to verify item is in cart or not
	if(!isset($this->items[$id])) {
		// add to cart
		$this->items[$id][name]  = $name;
		$this->items[$id][price] = $price;
		$this->items[$id][qty]	 = $qty; 
	}		
}

# Check To Verify Good Is Not Exist In Cart
function CRT_CheckItem($id) {
	if(isset($this->items[$id])) {
		return false;
	} elseif (!isset($this->items[$id])) {
		return true;
	}
}

# Update Items In Cart
function CRT_UpdateItemQTY ($id, $new_qty = 1) {
		// check to verify item is in cart or not
		if(isset($this->items[$id])) {
			// update item quantity in cart
			$this->items[$id][qty]	 = $new_qty; 
	}
}

# Remove Item From Cart
function CRT_DelItem($id) {
	if(isset($this->items[$id])) {
		unset($this->items[$id]); 
	}
}

# Delete Entire Cart Content
function CRT_DelCart() {
	$this->items = array();
	$this->total = 0;
}

# Calculate Total Cart Price
function CRT_TotalPrice() {
	$this->total = 0 ; // set total to zero in begining of calculations

	// check if cart is have any items
	if(isset($this->items)) {
		foreach ($this->items AS $key => $val) {
			$this->total += ($this->items[$key][price] * $this->items[$key][qty]);
		}
	}
	return $this->total;
}

# Show Cart
function CRT_ShowCart() {
	global $_CFG;
	$ret = "";
	if(isset($this->items)) {
		$count_items = count($this->items); // count number of items exist in cart
		if (!$count_items == 0) {
			$row_no = 1; // row number starts from 1				
			$ret .= "<table cellpadding=\"1\" cellspacing=\"2\" width=\"100%\" border=\"0\">
						<tr>
							<td width=\"5%\" bgcolor=\"#EFEFEF\" align=\"center\" style=\"font-weight: 600\">ردیف</td>
							<td width=\"50%\" bgcolor=\"#EFEFEF\" align=\"center\" style=\"font-weight: 600\">نام محصول</td>
							<td width=\"5%\" bgcolor=\"#EFEFEF\" align=\"center\" style=\"font-weight: 600\">تعداد</td>
							<td width=\"15%\" bgcolor=\"#EFEFEF\" align=\"center\" style=\"font-weight: 600\">قیمت</td>
							<td width=\"15%\" bgcolor=\"#EFEFEF\" align=\"center\" style=\"font-weight: 600\">قیمت کل</td>
							<td width=\"10%\" bgcolor=\"#EFEFEF\" align=\"center\" style=\"font-weight: 600\">حذف</td>
						</tr>\n
			";
			foreach ($this->items AS $id => $val) {
					$ret .= "	<tr>\n";					
					$ret .= "		<td style=\"border: 1px solid #EFEFEF;\" align=\"center\">" . STR_PNumber($row_no) . "</td>\n";
					$ret .= "		<td style=\"border: 1px solid #EFEFEF;\">" . $this->items[$id][name] . "</td>\n";
					$ret .= "		<td style=\"border: 1px solid #EFEFEF;\" align=\"center\">" . STR_PNumber($this->items[$id][qty]) . "</td>\n";
					$ret .= "		<td style=\"border: 1px solid #EFEFEF;\" align=\"center\">" . STR_PNumber($this->items[$id][price]) . "</td>\n";
					$ret .= "		<td style=\"border: 1px solid #EFEFEF;\" align=\"center\">" . STR_PNumber(($this->items[$id][price] * $this->items[$id][qty])) . "</td>\n";
					$ret .= "		<td style=\"border: 1px solid #EFEFEF;\" align=\"center\"><a href=\"" . $_CFG["site_url"] . "shop/cart.html?action=delete&id=" . $id . "\"><img class=\"bi\" src=\"" . $_CFG["site_url"] . "img/icon/del.jpg\"></a></td>\n";
					$ret .= "	</tr> \n";
					$row_no++; // add 1 to row number				
			}
			$ret .= "</table>";
			$ret .= "<table width=\"100%\"><tr><td align=\"left\" style=\"font-weight: 600;\">مجموع: <span style=\"font-weight: 600; color: red;\">" . STR_PNumber($this->CRT_TotalPrice()) . "</span> تومان</td></tr></table>";
			$ret .= "<table width=\"100%\"><tr><td>" . $_CFG["cart_desc"] . "</td></tr></table>";
		} else {
			$ret .= "<br><br><center><b>محصولی در سبد خرید موجود نیست.</center><br>";
			$ret .= "<table width=\"100%\"><tr><td>" . $_CFG["cart_desc"] . "</td></tr></table>";
		}
	}
	return $ret;
}	
} // END CLS_CART

?>

 

HERE IS THE cart.php where the class is called ( the session is created i another page) ->

<?php
$page = array(
  "name"  => "سبد خرید",
  "title" => "index",
  
  "domain" => "shop",
  "access" => "all",
  
  "script" => array(),
  "load"   => array(),
  "method" => array("shop"),
);
chdir("../");
include "./inc/global.php";



$action = $_GET["action"];
$id		= $_GET["id"];
$no		= $_GET["no"];

// SET DEFAULT $no VALUE
if ($no == "") {
$no = 1;
}

switch ($action) {
// ADD TO CART
case "add" :
$cart = $_DB->Query("SELECT `f_url`, `f_ftitle`, `f_price` FROM `er_shop` WHERE `f_url`=" . $id . "");
// CHECK IF $id IS EXIST IN DB
if ($id == $_DB->GetResultValue($cart, 0, "f_url")) {
	// CHECK IF GOOD EXIST OR NOT IN CART
	if ($_SESSION['cart']->CRT_CheckItem($id)) {
		$_SESSION['cart']->CRT_AddItem($id, $_DB->GetResultValue($cart, 0, "f_ftitle"), $_DB->GetResultValue($cart, 0, "f_price"), $no);
		$_PAGE->SetMethod("shop_cart_add");
	} 
	elseif (!$_SESSION['cart']->CRT_CheckItem($id)) {
	  	$_PAGE->SetMethod("shop_cart_exist");
	}
}
break;

// DELETE ITEM FROM CART
case "delete" :
	$_SESSION['cart']->CRT_DelItem($id);
	$_PAGE->SetMethod("shop_cart_del");
break;

// DELETE ENTIRE CART
case "reset" :
	$_SESSION['cart']->CRT_DelCart();
	session_destroy();
	$_PAGE->SetMethod("shop_cart_reset");
break;	
}

$_PAGE->NewContent("body");
$_PAGE->AddContent($_SESSION['cart']->CRT_ShowCart());
$_PAGE->AddWindow("سبد خرید", $_PAGE->GetContent());

$_PAGE->ShowPage();
?>

 

and heres go the creating sessions and object.

//CART
include "./inc/cls/cart.php";
if(session_id() == ""){
session_start();
}
if (!isset($_SESSION['cart']) || $_GET['reset'] == 1) {
    $_SESSION['cart'] = new CLS_CART();
}
//CART

thanks for reply,

 

I found a clue, the sessions only work one time, and then it will be deleted by server or something, but in local mode I dont have any problems and the whole site runs like dog as well as shopping cart!

 

can you give me more information about hard code ? or just an example in my own script. thanks.

 

I call this peace of code in every page of my site, is this the mean of hard coding?

<?php
//CART
include "./inc/cls/cart.php";
if(session_id() == ""){
session_start();
}
if (!isset($_SESSION['cart']) || $_GET['reset'] == 1) {
    $_SESSION['cart'] = new CLS_CART();
}
//CART
?>

I found another clue, the session is valid only on fires declare! I mean if I point the browser directly to the script that use class and add a query string to url it will adds the good in the cart, but after I want to add another good with same method, the session will become invalid and I get darn Fatal error: Call to a member function on a non-object .

 

please help I really lost my patient with this...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.