1

我正在编写将遍历一组页面的销售软件,在某些页面上列出要出售的项目,当您单击购买时,它基本上只是将一个隐藏变量传递到下一页以设置为会话变量,然后当你到最后它调用被报告给数据库。但是我的雇主希望我加入一个购物车,这个购物车应该显示你所购买的商品名称、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>";
            }

            }
        }

所以在这一点上,我不确定从这里到哪里去删除按钮。任何建议,将不胜感激。

4

1 回答 1

2

您可以在会话变量上使用数组,以便您可以将格式更改为

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

在你function 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);
于 2012-09-28T15:27:02.173 回答