Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete item from cart.

Status
Not open for further replies.

JamesManke

Programmer
Jul 1, 2004
54
CA
Hi there,
am new to php, so bare with me here.
I am trying to delete an item from my cart and am not having very much luck. I have tried unset() but leaves me with a zero value.

Probably easier to show you the code....
Here it is...

session_start();

echo"<form method='post' action='cart.php?update_cart=yes'>";

if($delete != "")
{
HERE IS WHERE I NEED HELP!
}

if($update_cart == "yes")
{
for($i = 0; $i < $ses_basket_items; $i++)
{

$old_amount = $ses_basket_amount[$i];
if ($_POST[$i] == 0)
{
$ses_basket_amount[$i] = 1;
}
else
{
$ses_basket_amount[$i] = $_POST[$i];
}
$ses_basket_price[$i]=$ses_basket_price[$i] + ($ses_basket_reg[$i] * ($ses_basket_amount[$i] - $old_amount));

}
}

if($id != "")
{
include "hook_up.inc";
$sql = "SELECT * FROM products WHERE id = $_GET[id]";

$results = mysql_query($sql);
$show2 = mysql_fetch_assoc($results);
$price = $show2['price'];
}

if ($basket!=""){
if (session_is_registered("ses_basket_items")){
$basket_position_counter=0;
$double=0;
if ($ses_basket_items>0){
foreach ($ses_basket_name as $basket_item){
if ($basket_item==$basket){
$double=1;
$basket_position=$basket_position_counter;
}
$basket_position_counter++;
}
}
if ($double==1){
$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;
}else{
$ses_basket_name[]=$basket;
$ses_basket_amount[]=1;
$ses_basket_price[]=$price;
$ses_basket_id[]=$id;
$ses_basket_reg[]=$show2[price];
$ses_basket_items++;
}
}else{
$ses_basket_items=1;
$ses_basket_name[0]=$basket;
$ses_basket_amount[0]=1;
$ses_basket_price[0]=$price;
$ses_basket_id[0]=$id;
$ses_basket_reg[]=$show2[price];
session_register("ses_basket_items");
session_register("ses_basket_name");
session_register("ses_basket_amount");
session_register("ses_basket_price");
session_register("ses_basket_id");
session_register("ses_basket_reg");
}
}
if($ses_basket_amount[0] != "")
{
if ($ses_basket_items>0){
for ($basket_counter=0;$basket_counter<$ses_basket_items;$basket_counter++){
$price=sprintf("%01.2f",$ses_basket_price[$basket_counter]);
$amount=$ses_basket_amount[$basket_counter];
$name=$ses_basket_name[$basket_counter];
echo "<input type='text' value='$amount' name='$basket_counter' size='3' /> $name @ $$ses_basket_reg[$basket_counter] per item = $$price <a href='cart.php?delete=$basket_counter'>Delete Item</a>";
echo "<BR>\n";
}
} else {
$ses_basket_items=0;
unset($ses_basket_name);
unset($ses_basket_amount);
unset($ses_basket_price);
unset($ses_basket_id);

}
if($ses_basket_price[0] != ""){
$total = $ses_basket_price;
$sub_total = sprintf("%01.2f",$total);
$sub_total_num = array_sum($total);
$the_num = $sub_total_num;
$the_number = $the_num;
$with_floor = floor($the_number);
$orig_len = strlen($the_number);
$floor_len = strlen($with_floor);
$total_missing = $floor_len - $orig_len;

if($total_missing == -3)
{
$total_num = $the_number;
}
if($total_missing == -2)
{
$total_num = $the_number . "0";
}
if($total_missing == 0)
{
$total_num = $the_number . ".00";
}

}
if($ses_basket_price[0] != "")
{
echo"<b>Subtotal = $$total_num</b>";
}

echo"
<br />
<input type='submit' value='Update Cart' />
</form>
";
}
else
{
echo"There are no items in your cart.<br />";
}
?>
<br /><a href="basket.php">Back Shopping</a>
 
If I may, I would like to offer a few constructive criticisms on your posted code:[ul][li]Indent your code. It simply makes structured code more readable and thus more maintainable.[/li][li]Your indentation style wanders quite a bit. In some lines you're using:
if($id != "")
{
in others:
if ($double==1){
If you pick a style and stick with it, it will make your code more readable and more maintainable.[/li][li]This section:if($total_missing == -3)
{
$total_num = $the_number;
}
if($total_missing == -2)
{
$total_num = $the_number . "0";
}
if($total_missing == 0)
{
$total_num = $the_number . ".00";
}
would be more readable written as a switch():
switch($total_missing)
{
case -3:
$total_num = $the_number;
break;

case -2:
$total_num = $the_number . "0";
break;

case 0:
$total_num = $the_number . ".00";
break;
}
It will also make your code more efficien.[/li][li]The PHP online manual on session_register() recommends against using session_register() and instead using direct references to the $_SESSION superglobal array[/li][/ul]

You've stated using unset() didn't work. On what did you use unset()?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
if($delete != "")
{
unset($ses_basket_items[$delete]);
unset($ses_basket_name[$delete]);
unset($ses_basket_amount[$delete]);
unset($ses_basket_price[$delete]);
unset($ses_basket_id[$delete]);
unset($ses_basket_reg[$delete]);
}

This is what I was using to remove an item from the cart. But this way is not working for me. I am looking for the proper way to delete an item in the array without messing it up. Is there a better way to do this, or should I saya proper way to do this?

ps - Thanks for the tips, I can use an constructive critism I can get, greatly appreciated;)
 
By not working I mean it is erasing the session variable using unset BUT when returning to the catalogue and trying to add some more items and it gives me a zero value. After adding a few more items it gives me this error ...

Warning: Division by zero in c:\program files\easyphp1-7\ on line 73

Perhaps you could run the script to see what I am talking about, thanx
 
How can we run the script, when we have no data to run it with?

I think you have to look at line 73, and the surrounding lines, are there any mathematical operators there, where there is division? (/).

If so, try to echo out that line, save the file and run the script again.

If it does not halt, try to echo out the variables which you try to run a division on, and I'm sure that you try to divide something on 0, which is not possible in math!

You can not do 10/0, 0/0, etc. that is division by zero.
How do you fix this?
You need to _validate_ your variables!

Olav Alexander Mjelde
Admin & Webmaster
 
can you print the arrays using print_r before the unset, do the unset and then print_r them again please and show us the result.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top