duandangqin0559 2012-09-28 15:09
浏览 97
已采纳

非常简单的购物车,删除按钮

Im writing sales software that will be walking through a set of pages and on certain pages there are items listed to sell and when you click buy it basically just passes a hidden variable to the next page to be set as a session variable, and then when you get to the end it call gets reported to a database. However my employer wanted me to include a shopping cart, and this shopping cart should display the item name, sku, and price of whatever you're buying, as well as a remove button so the person doing the script doesnt need to go back through the entire thing to remove one item. At the moment I have the cart set to display everything, which was fairly simple. but I cant figure out how to get the remove button to work. Here is the code for the shopping cart:

        $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>";
            }

            }
        }

So at this point Im not sure where to go from here for the remove button. Any suggestions would be appreciated.

  • 写回答

1条回答 默认 最新

  • dongyue1988 2012-09-28 15:27
    关注

    You can use arrays on session variables so you could change your format to

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

    in your function displaycart() add the element

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

    then when you delete you simply:

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能