...

View Full Version : shopping cart using sessions



balong3030
07-02-2005, 11:08 AM
I've revised my previous code and followed and modified sample code at a tutorial. the code is working in terms of adding. but i could not get it to delete and update quantity. suggestions pls.

here is the code. $price, $price2, $id, came from a link on a page.
e.g. <A HREF="minibasket3.php?id=1 & price=100 & price2=100 & basket=key">


<?php
session_start();
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
if ($basket!=""){
if (session_is_registered("ses_basket_items")){ // check if registered
$basket_position_counter=0; //if basket has item.
$double=0;
if ($ses_basket_items>0){
foreach ($ses_basket_name as $basket_item){
if ($basket_item==$basket){ //check for same entry.
$double=1;
$basket_position=$basket_position_counter;
}
$basket_position_counter++;
}
}
if ($double==1){ //update price for the same entry.
$oldamount=$ses_basket_amount[$basket_position];
$ses_basket_amount[$basket_position]++;
$amount=$ses_basket_amount[$basket_position];
$oldprice=$ses_basket_price[$basket_position];
$newprice=($oldprice/$oldamount)*$amount;
$ses_basket_price[$basket_position]=$newprice;
$total=$total+($oldprice/$oldamount);
/*foreach($ses_basket_price as $v){
$total = $total + $v;
}*/
}else{
$ses_basket_name[]=$basket;
$ses_basket_amount[]=1;
$ses_basket_price[]=$price;
$ses_basket_price2[]=$price2;
$ses_basket_id[]=$id;
$ses_basket_items++;
$total=$total+$price;
}
}else{
$ses_basket_items=1; //start basket, would not pass this block if $sesbasket_items is registered.
$ses_basket_name[0]=$basket;
$ses_basket_amount[0]=1;
$ses_basket_price[0]=$price;
$ses_basket_price2[0]=$price2;
$ses_basket_id[0]=$id;
$total=$price;
session_register("total");
session_register("ses_basket_items");
session_register("ses_basket_name");
session_register("ses_basket_amount");
session_register("ses_basket_price");
session_register("ses_basket_price2");
session_register("ses_basket_id");
}
}
?>
<!--// show basket-->
<table border=1>
<form action="<? $PHP_SELF; ?>" method="GET">
<tr><td> Qty </td><td> Item </td><td> item_price </td><td> price </td></tr>
<?
if ($ses_basket_items>0){
for ($basket_counter=0;$basket_counter<$ses_basket_items;$basket_counter++){
$price2=sprintf("%01.2f",$ses_basket_price2[$basket_counter]);
$price=sprintf("%01.2f",$ses_basket_price[$basket_counter]);
$amount=$ses_basket_amount[$basket_counter];
$name=$ses_basket_name[$basket_counter];
$total_price=$total;
?>
<tr>
<td><input type="text" name="quant" size="3" value="<? echo $amount; ?>"></td>
<td><? echo $name; ?></td>
<td><? echo $price2; ?></td>
<td bgcolor=#eeeeee><? echo $price; ?></td>
<td> <input name="act" type="hidden" value="upd"> </td>
<td> <input type="submit" name="submit" value="Update"></td>
<td> <a href="<? $PHP_SELF ?>?action=delete"> Remove </a></td>
</tr>
<? if($action=="delete"){
$total=$total-$price2;
unset($ses_basket_price2[$basket_counter]);
unset($ses_basket_price[$basket_counter]);
unset($ses_basket_amount[$basket_counter]);
unset($ses_basket_name[$basket_counter]);
}
?>
<? } ?>
<?
} else {
$ses_basket_items=0;
unset($ses_basket_name);
unset($ses_basket_amount);
unset($ses_basket_price);
unset($ses_basket_id);
}
?>
<tr><td colspan=3> total price is: </td><td bordercolor=#ff0000><? echo $total;?></td></tr>
<tr><td colspan="4"><a href="store2.php"> continue shopping </a></td></tr>
</form>
</table>
</body>
</html>

chump2877
07-02-2005, 01:40 PM
the code is working in terms of adding. but i could not get it to delete and update quantity.

Could you be a little more specific with the problem you're having...and could you please put all that code between some [PHP] tags....

balong3030
07-02-2005, 02:24 PM
the problem is deleting in array.

if I click on remove, it will remove everything in the ff arrays:

These is the block it goes through when deleting:


<?
if($action=="delete"){
$total=$total-$price2;
unset($ses_basket_price2[$basket_counter]);
unset($ses_basket_price[$basket_counter]);
unset($ses_basket_amount[$basket_counter]);
unset($ses_basket_name[$basket_counter]);
}
?>


I'm expecting it to remove only the rows displayed but instead it deletes everything in the array including other rows.

balong3030
07-02-2005, 02:28 PM
its not only deleting the row in line with the remove link, but it also removes the row above it. And the $total is returning zero which is not supposed to .... not unless there are no other items in the ff arrays.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum