Josh Gomez Josh Gomez - 2 months ago 12
PHP Question

How do I use $_SESSION to store items in an array and trigger a button swap?

so I'm trying to store shopping cart items by there ID. Once this happens a button needs to appear "Remove from Cart", if I click this button a new button will appear "Add to Cart" but I'm a little bit lost on it from this point. Im new on here so please excuse my mistakes. My code is below:

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

}

Answer Source

In your case here what should be done:

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>`    
}