I'm using sessions for adding products to a shopping cart. When a customer hits the "Order this Item" from a particular product page, they are directed to orderitem.php?prodID=blah

This page has a long form which is filled out by the customer. At the end of the form is the typical "Add to Cart" button.

So now i need to determine what to do when this button is clicked. It seems like most shopping carts then take you to the actual cart and show you the contents. This is what I'd like to do. However, I cannot figure out in my tiny head how to make it happen, and I'll explain why.

What I can figure out is the following. When a customer hits "Add to Cart", I can send them to a "Added to Cart" page, showing the specifics of the item added. On this page, I can then pull whatever necessary values from $POST and save them to my session.

	$prodID = $_POST['prodID'];
	$productDetails = array("qty"=>$_POST['qty'],
	                        "pColor"=>$_POST['pColor'],
	                        "iColor"=>$_POST['iColor'],
	                        "IHdate"=>$_POST['IHdate']);
	$_SESSION['cart'][$prodID] = $productDetails;
	if (isset($_SESSION['numitems'])) {
		$_SESSION['numitems']+=1;
	}
	else {
		$_SESSION['numitems'] = 1;
	}

Then on this page, I can give the customer a link to view the contents of their cart. The mycart.php page will basically have a loop that runs through the SESSION and prints all the cart contents. This all makes sense to me.

But how can I combine this into one step? How do I add an item to the cart on the cart page itself.

Would I do this by testing the $POST function for any of the possible values that would be there if indeed the link came from an "Order this Item" page? Could I do a the following:

if (isset($_POST['qty']) {
     // this signals that we came to mycart.php from an ordering page, or
     // more specifically, from an order that was posted.
     // So FIRST, add the current posted order to the session array
}

THEN follow the normal mycart.php implementation.

Would that be how it works?

Thanks.

    Also, as a follow up question, note how I save the individual order information into the "cart" ($_SESSION).

    I save all of the posted data into an array, $productdetails, and then I save this $productDetails into my $SESSION array as follows:

    $_SESSION['cart'][$prodID] = $productDetails;

    So you see how I reference the current $prodID in order to save the $productDetails in the array.

    Now here's my follow up question. In the mycart.php page, where I want to display the contents of this cart, even though the $_SESSION array is indexed with $prodID's, can I still iterate through this array with a counter variable? Can I do a simply for loop?

    for ($i=0; $i <= $_SESSION['numitems']; $i++) {
         // do stuff in here referencing $_SESSION['cart'][$i]
         // thus allowing me to go through the array and print out the cart details
    }

    Will this still work, meaning my use of the $i counter variable, even though those spots are indexed with a string?

    Thanks.

      You can combine the add to cart and display cart into one page. But having separate add and display pages is fine too.

      product form add to cart button -> posts to add.php which inserts data -> redirect to cart display page

      In other words add.php does no output! At end it redirects user to cart display page using header().

        I think they way I will set it up is to simply combine the two pages. So once the user hits "Add to Cart", they will be taken to the mycart.php page.

        You confirmed that was possible, which was my first ? on this thread.

        My second question is now, how do I do this. Could someone please reference my second post and give some input.

        Thanks.

          foreach( $_SESSION['cart'] as $id => $details ) {
            //$id is product id
            //$details is your array of product details
          }
          

            So, as I to want to combine these two pages/concepts into one page, when I get to the mycart.php page, how do i code it to tell whether or not it arrived on this page as a result of a product addition or a result of simply hitting "My Cart"?

            Again, does it make sense to test any of the $POST variables that would be set IF we arrived to the mycart.php page as a direct result of submitting an order form? Would that work?

            if (isset($_POST['qty']) {
                 // this signals that we came to mycart.php from an ordering page, or
                 // more specifically, from an order that was posted.
                 // So FIRST, add the current posted order to the session array
            }

            Does that type of code make any sense?

            Thanks for your help.

              I just did this yesterday, convert 3 pages into a single cart.php...

              Basic idea is to set some value you can interrogate to figure out what "command" the user is trying to do (add to cart, recalculate, continue, empty cart, checkout, view cart), the default will be to simply view cart contents.

              I took a cheap way out here by looking at text of submit button. You could do something a little more sophisticated using javascript to set the "command" when user clicks submit (hint each submit button calls some javascript func to set a field in the form that represents the command). But I didn't want to use any javascript in this case.

              
              $submit = $_POST['submit'];
              
              if( $submit == 'Add to cart' ) {
                //do add to cart
                //cart contents displayed at end of script
              } elseif( $submit == 'Continue') {
                header( "Location: index.php"); //or wherever
                exit;
              } elseif( $submit == 'Recalculate') {
                //get POST vars and recalculate
                //dont exit at end, fall thru
              } elseif( $submit == 'Empty cart') {
                //empty the cart arrays
                header( "Location: emptycart.php"); 
                exit;
                //or you can set the correct template to display
              } elseif( $submit == 'Check out') {
                header( "Location: checkout.php");
                exit;
              }
              
              //get items and calculate totals
              //include template to display cart (I use tempaltes to separate presentation from logic)
              
              

              Actually, since I've changed the site in question to use ONLY cookies for sessions, I need to break this out into add.php and cart.php again, that way if someone has cookies disabled and adds a product, when cart.php displays it can realize: "user came from add.php but cart is empty...they probably don' t have cookies enabled..." and show user a "cookies required" message. My programs talk to me. Is that good?

                yeah that helps. does my isset method also work? I'm not familiar with isset, but by name, i guess it just checks to see if a value is "set" or not.

                Now here's the main detailed question on that, once a user "posts" a form and they go to the next page, on that next page, the details of the form can be found in the $_POST global. So the isset will be okay here.

                Now, does that $_POST global clear itself once the user goes to any other page by ANY means other than clicking a form where method=post? I hope the answer is yes.

                Because if the answer is no, then that means that $_POST basically stays "set" once it is set once, or until it is filled with a new set of data.

                Thanks.

                  $_POST values come from a POST HTTP request. Wouldn't make sense to fill it up and keep it filled. In fact, PHP remembers NOTHING from one request to another.

                  As far as isset or any other function, check the manual. http://us2.php.net/manual/en/function.isset.php

                  I'm not sure by which isset() you mean...I don't think you want to check item quantities to figure out what is going on.

                    It's not a matter of checking quantities to see what's going on. I asked in the first post if it made sense to check ANY of the $_POST values to see if they were set.

                    Cause if ANY of the posted values were set (assuming they were required values in the validation of the form), then wouldn't that then mean that the user did indeed arrive at mycart.php from posting an order form?

                    One could also check if the submit was "set", as you somewhat used in your example,

                    isset($_POST['submit']) {
                    //do stuff
                    }

                    So will this work?

                      Write a Reply...