to all guru
this script is for shopping cart and still got some bugs, The basket is a class
HERE IS The error i got
Notice: Undefined variable: basket in c:\inetpub\wwwroot\cart\insertsample.php on line 18
Fatal error: Call to a member function on a non-object in c:\inetpub\wwwroot\cart\insertsample.php on line 22
here is the full source code
cart_class.php
<?
class Basket {
var $basket_count;
var $basket_item_id;
var $basket_item_album;
var $basket_item_img;
var $basket_item_shortdesc;
var $basket_item_quantity;
var $basket_item_price;
function Basket() {
$this->basket_count=0;
}
function Add_Item($id,$img,$shortdesc,$quantity=1,$price=0) {
$this->basket_item_id[$this->basket_count]=$id;
// $this->basket_item_artist[$this->basket_count]=$artist;
$this->basket_item_album[$this->basket_count]=$album;
$this->basket_item_img[$this->basket_count]=$img;
$this->basket_item_shortdesc[$this->basket_count]=$shortdesc;
$this->basket_item_quantity[$this->basket_count]=$quantity=1;
$this->basket_item_price[$this->basket_count]=$price;
//$this->basket_item_wt[$this->basket_count]=$wt;
$this->basket_count++;
return ($this->basket_count-1);
}
function Del_Item($pos) {
$this->basket_item_id[$pos]="";
}
function Get_Item_id($pos) {
return $this->basket_item_id[$pos];
}
/* function Get_Item_artist($pos) {
return $this->basket_item_artist[$pos];
} */
function Get_Item_album($pos) {
return $this->basket_item_album[$pos];
}
function Get_Item_img($pos) {
return $this->basket_item_img[$pos];
}
function Get_Item_shortdesc($pos) {
return $this->basket_item_shortdesc[$pos];
}
function Get_Item_price($pos) {
return $this->basket_item_price[$pos];
}
function Get_Item_quantity($pos) {
return $this->basket_item_quantity[$pos];
}
// function Get_Item_wt($pos) {
// return $this->basket_item_wt[$pos];
// }
function Set_Item_Quantity($pos,$quantity) {
$this->basket_item_quantity[$pos]=$quantity;
}
function Enum_Items($start=false) {
static $current;
if ($current>=$this->basket_count) return -1;
if (!$start) {
$current++;
} else {
$current=0;
}
while (($this->basket_item_id[$current]=="") && ($current<$this->basket_count)) {
$current++;
}
return ($current<$this->basket_count) ? $current : -1;
}
function Empty_Basket() {
$this->basket_count=0;
}
function Get_Basket_Count() {
$num=0;
for ($i=0;$i<$this->basket_count;$i++) {
if ($this->basket_item_id[$i]!="") $num++;
}
return $num;
}
}
?>
NOW TO add product on cart
add.php
<?
require("cart_class.php");
session_name("mysession");
session_start();
if (!session_is_registered("basket")) {
$basket=new Basket;
session_register("basket");
}
$myid="";
$myquant="";
$zpos="";
$itemid=@$_GET["itemid"];
if ($basket->Get_Basket_Count()>0) { # are there items in the basket
$pos = $basket->Enum_Items(true);
while ($pos>=0) {
if ($basket->Get_Item_id($pos)==$itemid)
{
$basket->Set_Item_Quantity($pos,($basket->Get_Item_quantity($pos)+1));
$addprod="no";
}
$myid=$basket->Get_Item_id($pos);
$myquant=$basket->Get_Item_quantity($pos);
$zpos=$pos;
$pos = $basket->Enum_Items();
}
}
if (@$addprod!="no")
{
// please modify the query here to get the result
/*
$SQL = "select recordid,album,thumbnail,shortdesc,price from tblproducts WHERE recordid='$itemid'";
$result = mysql_query($SQL) or die("Invalid query: " . mysql_error());
$row = mysql_fetch_object($result);
*/
// to add item on basket this is the original since data coming from your table
//$basket->Add_Item($itemid,$row->album,$row->thumbnail,$row->shortdesc,$row->qty,$row->price);
// end of original code
// since i dont have any idea on your products so i put dummy data in this cart
$basket->Add_Item($itemid,"this is image","This is a short description",1,1.55);
}
echo "<script>location.replace('insertsample.php');</script>";
//header("Location:insertsample.php");
?>
AND i created a sample page
insertsample.php
<?
require("cart_class.php");
session_name("mysession");
session_start();
if (!session_is_registered("basket"))
{
$basket=new Basket;
session_register("basket");
}
?>
<html>
<head>
<title>Sample page to insert products</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<? echo $basket->basket_count;?>
<A href="add.php?itemid=1">Item 1</A><br>
<A href="add.php?itemid=2">Item 2</A><br>
<?
$basket->Add_Item(1,"this is image","This is a short description",1,1.55);
?>
</body>
</html>