Update Shopping basket

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

Thanks ScallioXTX. I have had a bad experience in the past for ignoring advice from much more knowledgeable people like yourself and Jake. I have completed this website now however as many people have already suggested i need to tweak the code making it stronger having more integrity.

I think the best thing for me to do is try to understand the code you guys have suggested and try to put it into practice. Then if i have any problems i can ask you and post questions along the way. At the end of the day it will be for a good cause and i will be benefiting from it.

Thanks again.

Bill

  1. Your update function makes no sense to me. Why would you want to empty the cart and built it up again when you can also just updatet the quantities like so:

foreach($_POST['ID'] as $i=>$id) {
  foreach($_SESSION['ID'] as $j=>$sessionId) {
    if ($id==$sessionId && isset($_SESSION['quantity'][$j])) {
      $_SESSION['quantity'][$j] = $_POST['quantity'][$i];
    }
  }
}

  1. I really suggest you use a different storing mechanism for the items in the cart, as Jake Arkinstall already suggested to you in this post. That would make all your functions way easier, as you would not need nested foreach loops like in the code above (believe me, I can hardly follow that code myself).

  2. Don’t use javascript to redirect the browser, use


header('Location: '.$loc);