hi all!
i am building a shopping cart , in this when a user click on an item the catname,productname,optionanme(if any) price will be selected, i m incrementing with the quantity but when i remove the quanity and it goes to morethan -1 and keep on , my all variable in save in session can anybody tell how to destory the specific array if it goes to 0 .
my code is here.
<?php
class Cart
{
// invalid member declarations:
public $ProductId;
public $OptionId;
public $Name;
public $Oname;
public $Quantity;
public $Price;
function Cart($catn,$tid,$oid,$name,$oname,$qa,$pr){
$this->ProductId=$tid;
$this->Catn=$catn;
$this->OptionId=$oid;
$this->Name=$name;
$this->Oname=$oname;
$this->Quantity=$qa;
$this->Price=$pr;
}
}
if(isset($_REQUEST['cat_ids'])){
$sql = "select cat_ids,cat_name from category where cat_ids =" .$_REQUEST['cat_ids'];
$result = mysql_query($sql,$db);
$row = mysql_fetch_array($result);
$catn= $row['cat_name'];
}
if(isset($_REQUEST['prod_id'])){
$product_id=$_REQUEST['prod_id'];
$sql1= "select prod_id,prod_desc,prod_name from product where prod_id=".$_REQUEST['prod_id'];
$test = mysql_query($sql1,$db);
$row1 = mysql_fetch_array($test);
$prod_name = $row1['prod_name'];
}
$option_id="";
if(isset($_REQUEST['optionid']) && $_REQUEST['optionid']!=""){
$option_id=$_REQUEST['optionid'];
$sql1= "select price,name from options inner join prices on options.id=prices.option_id where options.id=".$_REQUEST['optionid'];
$test = mysql_query($sql1,$db);
$row1 = mysql_fetch_array($test);
$price = $row1['price'];
$oname = $row1['name'];
}else{
if(isset($_REQUEST['prod_id'])){
$sql1= "select price from prices where product_id=".$_REQUEST['prod_id'];
$test = mysql_query($sql1,$db);
$row1 = mysql_fetch_array($test);
$price = $row1['price'];
}
}
$arr=array();
if(isset($_SESSION['cart']) ){
$arr=$_SESSION['cart'];
}
if(isset($_REQUEST['prod_id'])){
if(!isset($_SESSION['cart']) ){
$cart=new Cart($catn,$product_id,$option_id,$prod_name,$oname,1,$price);
array_push($arr,$cart);
$_SESSION['cart']=$arr;
}else{
$arr=$_SESSION['cart'];
$found=false;
foreach($arr as $key => $value) {
if($value->ProductId==$product_id && ($option_id=="" || $value->OptionId==$option_id)){
if(isset($_REQUEST['remove']) && $_REQUEST['remove']==1)
$value->Quantity-=1 ;
else
$value->Quantity+=1;
$found=true;
}
}
if(!$found){
$cart=new Cart($catn,$product_id,$option_id,$prod_name,$oname,1,$price);
array_push($arr,$cart);
}
$_SESSION['cart']=$arr;
}
}
?>
thanks in advance.
Delete an item in session array.
Moderator: General Moderators
Re: Delete an item in session array.
Code: Select all
unset($_SESSION['cart'][cartidentifier]);