如何使用$ _SESSION在数组中存储项目并触发按钮交换?

时间:2017-08-17 10:16:08

标签: php

所以我试图通过ID来存储购物车项目。一旦发生这种情况,按钮需要显示“从购物车中删除”,如果我点击此按钮,则会出现一个新按钮“添加到购物车”,但是从这一点上我有点迷失了。我是新来的,请原谅我的错误。我的代码如下:

session_start();

$items[] = $_POST['add'];

if (isset($_POST['add'])) {
   $_SESSION['cart'][]=$_POST['add'];

} else if (isset($_POST['remove'])) {

unset ($_SESSION['cart'][$_POST['remove']]);
}


foreach($vend as $vendID=> $items) {
 echo "<form action='vend.php' method='post'>";
 echo "<article id ='vend-$vendID'>";
 echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>";
 echo "<div class ='item-no'>";
 echo "<p class = 'pro-id'><b>Product ID:  </b>{$vendID}</p></div>";
 echo "</div>";
 echo "<div class ='img-div'>";
 echo "<img src=../images/{$items['img']} alt='' height='196' width='200'></div>";

 echo "<div class='item-p'>";
 echo "<p>{$items['desc']}</p></div>";

 echo "<div class='pricing'>";
 echo "<p><b>Price: $</b>{$items['price']}</p></div>";
 //echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>";


 if(isset($_POST['add']) && ($_SESSION['cart'] == $vendID)) {

echo "<button name='remove' type='submit' value='$vendID'>Remove from Cart</button>";
 }


else {


echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>"; 

}       

3 个答案:

答案 0 :(得分:0)

要从购物车中删除商品,您将根据$ vendID变量访问该商品。这将要求您的项目作为数组存储在$_SESSION['cart']中:

$_SESSION['cart'][0] = "item 1";
$_SESSION['cart'][1] = "item 2";
...

但稍后在您的代码中,您正在访问$_SESSION['cart'],就好像它只是一个值而非数组

使用以下行添加代码中的项目:

$_SESSION['cart'][]=$_POST['add'];

这导致如下:

//empty cart
$_SESSION['cart'][0] = $vendid;

答案 1 :(得分:0)

在你的情况下,应该做什么:

session_start();
/* Check if $_SESSION['cart'] exists */
if (!isset($_SESSION['cart'])) {
    /* Init cart as empty array */
    $_SESSION['cart'] = [];
}

if (isset($_POST['add']) && 0 < $_POST['add']) {
    /* Add product id to session cart */
    $_SESSION['cart'][$_POST['add']] = 1;
} else if (isset($_POST['remove']) && 0 < $_POST['remove']) {
    /* Remove product id from session cart */
    unset($_SESSION['cart'][$_POST['remove']]);
}
// I check with `0 < $id` because ids are always positive

foreach($vend as $vendID=> $items) {
    echo "<form action='vend.php' method='post'>";
    echo "<article id ='vend-$vendID'>";
    echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>";
    echo "<div class ='item-no'>";
    echo "<p class = 'pro-id'><b>Product ID:  </b>{$vendID}</p></div>";
    echo "</div>";
    echo "<div class ='img-div'>";
    echo "<img src=../images/{$items['img']} alt='' height='196' width='200'></div>";

    echo "<div class='item-p'>";
    echo "<p>{$items['desc']}</p></div>";

    echo "<div class='pricing'>";
    echo "<p><b>Price: $</b>{$items['price']}</p></div>";

    if(isset($_SESSION['cart'][$vendID])) {
        // you have `$vendID` in session cart - then show Remove button
        echo "<button name='remove' type='submit' value='$vendID'>Remove from Cart</button>";
    }  else {
        // you DON'T have `$vendID` in session cart - then show Add button
        echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>"; 
    }   
    // Also don't forget to close `</form>`    
}

答案 2 :(得分:0)

原来我需要一个下划线,我有$ POST而不是$ _POST现在可以使用了,谢谢大家:)