简体   繁体   中英

add to cart using session ( delete ) php

How to delete specific item using session array. I've tried many time but my code still cant work. Can anyone help me to check my Code? As the Remove Cart hyperlink pass the Product id that going to be delete. but it cannot delete anyway. i have no idea where the error is .

  cart.php if (isset($_POST['lol'])) { if (isset($_SESSION['cart']) === FALSE) { $_SESSION['cart']=array(); } $proid=$_REQUEST["proid"]; array_push($_SESSION['cart'],$proid); } $total=0; if (isset($_SESSION['cart'])) { $total = 0; foreach($_SESSION['cart'] as $proid ) { $results = mysqli_query($con,"Select * from product where Product_ID = '$proid'"); $myrow = mysqli_fetch_array($results); $price = $myrow['Product_Price']; $total =$total + $price; ?> <li class = "cartlist"><?php echo '<img src="data:image/jpeg;base64,' . base64_encode($myrow['Product_Pic']) . '" width="196" height="120">';? ><p><span style = "font-weight: bold; font-size: 1.2em;"> <?php echo $myrow["Product_Name"];?></span></br /> RM <?php echo $myrow["Product_Price"];?></br /> <?php echo $myrow["Product_Size"];?>MB <br/> <a href="cart.php?proid=<?php echo $proid;?>?action=remove">Remove From Cart</a></p> </li> <?php } } ?> <?php if (isset($_GET['proid'])) $proid=$_REQUEST["proid"]; else $proid = 1; if (isset($_GET['action'])) $action =$_REQUEST['action']; switch ($action) { case "remove" : if (isset($_SESSION['cart']['$proid'])) { $_SESSION['cart']['$proid']--; unset($_SESSION['cart']['$proid']); } break; case "empty" : unset ($_SESSION['cart']); break; } ?> 

The problem here is that you do not understand how arrays work in php.

Everything you store in an array is a key => value pair, even if you don't esplicitly set a key, php will set an integer value for you.

At some point you add a product id in your cart, like this:

array_push($_SESSION['cart'],$proid);

this can also be written like this: $_SESSION['cart'][] = $proid;

(And if you look at array_push in the php documentation, it says it's best to write it this way) This is an assignment, you are adding an element to the array, without specifying the key.

And here:

foreach($_SESSION['cart'] as $proid )

you are looping through the array values, ignoring the keys.

These lines of code suggest that the cart array will look like this:

array(
    0 => 'id-for-product-one',
    1 => 'id-for-product-two'
);

When you are trying to remove a product from the array, instead you are trying to look for the product as if that id you previously assigned as a value is now the key:

if (isset($_SESSION['cart']['$proid']))

This line has actually two mistakes.

The first is that by using single quotes the value of the string '$proid' is actually $proid . If you want to pass the value contained in $proid you can just omit the quotes, or in case you are building a more complex string you want the doublequotes (or use the . operator for string concatenation).

The second is that there is no array key for $prodid .

Given an array $a = array('key' => 'value') if you want to get 'value' , you write $a['key'] , what you are doing there is $a['value'] .

The solutions here are two:

One is to assign the id both as a key and as a value.

The other is to use array_search to get the key of the element you want to remove and work from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM