简体   繁体   English

使用会话添加到购物车(删除)php

[英]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. 随着“删除购物车”超链接传递将要删除的产品ID。 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. 这里的问题是您不了解数组如何在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. 您存储在数组中的所有内容都是key => value对,即使您没有私下设置键,php也会为您设置一个整数值。

At some point you add a product id in your cart, like this: 有时您会在购物车中添加产品ID,如下所示:

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

this can also be written like this: $_SESSION['cart'][] = $proid; 也可以这样写:$ _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. (如果您查看php文档中的array_push ,则表示最好用这种方式编写)。这是一种分配,是在数组中添加一个元素,而不指定键。

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: 这些代码行表明cart数组将如下所示:

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: 当您尝试从数组中删除产品时,您正在尝试查找产品,就像以前分配给该值的id现在是键一样:

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 . 第一个是通过使用单引号引起来,字符串'$proid'值实际上是$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). 如果要传递$ proid中包含的值,则可以省略引号,或者如果要构建更复杂的字符串,则需要双引号(或使用.运算符进行字符串连接)。

The second is that there is no array key for $prodid . 第二个是$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'] . 给定一个数组$a = array('key' => 'value')如果要获取'value' ,则编写$a['key'] ,在其中执行的操作就是$a['value']

The solutions here are two: 这里的解决方案有两种:

One is to assign the id both as a key and as a value. 一种是将ID既分配为键又分配为值。

The other is to use array_search to get the key of the element you want to remove and work from there. 另一种方法是使用array_search获取要删除的元素的键并从那里开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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