0

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

}       
Josh Gomez
  • 15
  • 5
  • looks to me as this post would answer your question [html-php-form-input-as-array](https://stackoverflow.com/questions/20184670/html-php-form-input-as-array) – chilly Aug 17 '17 at 10:24

3 Answers3

0

To remove an item from your cart you are accessing the item based on your $vendID variable. Which would require your item to be stored in $_SESSION['cart'] as an array:

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

but later in your code you are accessing $_SESSION['cart'] as if it is just a value not an array

adding an item in your code is done with the following line:

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

This results in something like this:

//empty cart
$_SESSION['cart'][0] = $vendid;
chilly
  • 304
  • 2
  • 13
0

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>`    
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

Turns out I needed a underscore , i had $POST instead of $_POST and it works now, thanks all :)

Josh Gomez
  • 15
  • 5