gpt4 book ai didi

php - 很简单的购物车,移除按钮

转载 作者:行者123 更新时间:2023-11-29 06:56:06 25 4
gpt4 key购买 nike

我正在编写销售软件,它将遍历一组页面,在某些页面上列出了待售商品,当您单击购买时,它基本上只是将一个隐藏变量传递到下一页以设置为 session 变量,然后当您到达结尾时,它的调用会报告给数据库。

但是我的雇主希望我包括一个购物车,这个购物车应该显示商品名称、sku 和您要购买的任何商品的价格,以及一个删除按钮,这样执行脚本的人就不会需要回顾整个事情才能删除一个项目。

目前我已将购物车设置为显示所有内容,这相当简单。但我不知道如何让删除按钮起作用。这是购物车的代码:

$total = 0;
//TEST CODE:
$_SESSION['itemname-addon'] = "Test addon";
$_SESSION ['price-addon'] = 10.00;
$_SESSION ['sku-addon'] = "1234h";

$_SESSION['itemname-addon1'] = "Test addon1";
$_SESSION ['price-addon1'] = 99.90;
$_SESSION ['sku-addon1'] = "1111";

$_SESSION['itemname-addon2'] = "Test addon2";
$_SESSION ['price-addon2'] = 19.10;
$_SESSION ['sku-addon2'] = "123";
//end test code

$items = Array
(
"0"=> Array
(
"name" => $_SESSION['itemname-mo'],
"price" => $_SESSION ['price-mo'],
"sku" => $_SESSION ['sku-mo']
),
"1" => Array
(
"name" => $_SESSION['itemname-addon'],
"price" => $_SESSION ['price-addon'],
"sku" => $_SESSION ['sku-addon']
),
"2" => Array
(
"name" => $_SESSION['itemname-addon1'],
"price" => $_SESSION ['price-addon1'],
"sku" => $_SESSION ['sku-addon1']
),
"3" => Array
(
"name" => $_SESSION['itemname-addon2'],
"price" => $_SESSION ['price-addon2'],
"sku" => $_SESSION ['sku-addon2']
)

);

$a_length = count($items);

for($x = 0; $x<$a_length; $x++){
$total +=$items[$x]['price'];
}
$formattedtotal = number_format($total,2,'.','');
for($i = 0; $i < $a_length; $i++){
$name = $items[$i]['name'];
$price = $items[$i]['price'];
$sku = $items[$i]['sku'];
displaycart($name,$price,$sku);
}
echo "<br />
<b>Sub Total:</b>
$$formattedtotal";

function displaycart($name,$price,$sku){

if($name != null || $price != null || $sku != null){

if ($name == "no sale" || $price == "no sale" || $sku == "no sale"){
echo "";
}
else{
$formattedprice = number_format($price,2,'.','');
echo "$name: $$formattedprice ($sku)";
echo "<form action=\"\" method=\"post\">";
echo "<button type=\"submit\" />Remove</button><br />";
echo "</form>";
}

}
}

所以在这一点上,我不确定从这里到哪里去删除按钮。

最佳答案

您可以在 session 变量上使用数组,这样您就可以将格式更改为

$_SESSION[items][] = ("name"=>$name, "sku"=>$sku, "price"->$price)

在你的函数displaycart()中添加元素

echo "<input type='hidden' name='delete_sku' value='$sku'>\n";

然后当你删除你时:

$delete_sku = $_POST[delete_sku]; // the posted ID sent from delete form
$i = 0;
foreach ($_SESSION[items] as $item) {
if ($item['sku'] == $delete_sku) {
unset ($_SESSION[$i]); // remove the item from the session array
}
$i++;
}
print_r ($_SESSION);

关于php - 很简单的购物车,移除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12642644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com