Hey,
I am trying to update a shopping basket through a session. Lets suppose i have multiple items in my basket and you come to this page (See Image Attached)
I have an update button and when this is pressed i want to update the current shopping basket with the values in the <select> dropdowns.
Now if the values remain the same and update is clicked then nothing should happen but if i select a different number of quantities then i want to update.
I have this code on the page for the <select>
<select name="quantity">
<?php
for($ie=1;$ie<=12;$ie++)
{
if($ie == $_SESSION['quantity'][$i]){
echo "<option selected=\\"selected\\" value=\\"$ie\\">$ie</option>";
}else{
echo "<option value=\\"$ie\\">$ie</option>";
}
}
?>
</select>
Then in my controller i do this:
if(isset($_POST['update'])){
session_start();
$cart->UpdateCart();
}
‘update’ is the form submit button. And then in my class i have the following functions which you may find useful for answering this question:
public function AddToCart(){
!isset($_SESSION['ID']) ? $_SESSION['ID'] = array() : '';
!isset($_SESSION['theName']) ? $_SESSION['theName'] = array() : '';
!isset($_SESSION['quantity']) ? $_SESSION['quantity'] = array() : '';
!isset($_SESSION['price']) ? $_SESSION['price'] = array() : '';
!isset($_SESSION['image']) ? $_SESSION['image'] = array() : '';
array_push($_SESSION['ID'], $_POST['ID']);
array_push($_SESSION['theName'], $_POST['theName']);
array_push($_SESSION['quantity'], $_POST['quantity']);
array_push($_SESSION['price'], $_POST['price']);
array_push($_SESSION['image'], $_POST['image']);
$loc = $_SERVER['HTTP_REFERER'];
echo "<script>window.location.href='".$loc."'</script>";
}
public function UpdateCart(){
isset($_SESSION['ID']) ? $_SESSION['ID'] = array() : '';
isset($_SESSION['theName']) ? $_SESSION['theName'] = array() : '';
isset($_SESSION['quantity']) ? $_SESSION['quantity'] = array() : '';
isset($_SESSION['price']) ? $_SESSION['price'] = array() : '';
isset($_SESSION['image']) ? $_SESSION['image'] = array() : '';
array_push($_SESSION['ID'], $_POST['ID']);
array_push($_SESSION['theName'], $_POST['theName']);
array_push($_SESSION['quantity'], $_POST['quantity']);
array_push($_SESSION['price'], $_POST['price']);
array_push($_SESSION['image'], $_POST['image']);
$loc = $_SERVER['HTTP_REFERER'];
echo "<script>window.location.href='".$loc."'</script>";
}
I am aware i should not have price in my [‘POST’] i will be changing this soon.
Anyway when i click on update, from the image attached it leaves me with only one item left in the basket, it does not update all of the items. So from the image if i clicked on Update, the King Stud would disappear and would only leave the Wingman T-Shirt. Why is this?
Can you see what i am trying to do?
Is there a solution?
Thanks again