Page 2 of 2

Posted: Sun Apr 01, 2007 3:32 am
by visitor-Q
i seem to be doing all the posting in this thread so i'll add another update:

itemTemplate.php

Code: Select all

<?php
        session_start();

        /*if this is the first page the user sees, register the session id now*/
        if(!isset($_SESSION['session_id'])){ $_SESSION['session_id'] = session_register(); }    

       /*
        * parameter 1: name of item
        * parameter 2: id of the item (found in the sybase database)
        * parameter 3: price of the item
        * parameter 4: how many come with this purchase?
        * parameter 5: ingredients that come with this item (found in sybase database)
        * parameter 5: all the ingredients that can come with this item (found in sybase database)
        *
        * prints the name and hidden input field values for this item.
        * populates the checkbox form and compares the ingredient 
        * being populated with the standard ingredients of the item. if
        * there is a match, precheck the ingredient. print price.
        * close form.
        *
        * set standard ingredients array. set all possible ingredients array
        */
        function populateForm($name, $id, $price, $quantity, $standard_ingredients, $all_ingredients){
                echo "
                        <b>{$name}</b>
                        <form action=\"cart.php\" method=\"post\">
                        <input type=\"hidden\" name=\"id\" value=\"{$id}\">
                        <input type=\"hidden\" name=\"name\" value=\"{$name}\">
                        <input type=\"hidden\" name=\"price\" value=\"{$price}\">
                        <input type=\"hidden\" name=\"quantity\" value=\"{$quantity}\">
                \n";

                foreach($all_ingredients as $a_ingredient){
                        echo "\t\t\t<input type=\"checkbox\" name=\"ingredients[]\" value=\"{$a_ingredient}\"";
                                foreach($standard_ingredients as $s_ingredient){
                                        (($s_ingredient == $a_ingredient) ? (" CHECKED") : (""));
                                }
                        echo ">{$a_ingredient}\n";
                }

                echo "
                        <br /><b>$". number_format($price, 2) ."</b>
                        <br /><input type=\"submit\" value=\"Add to Cart\">
                        </form>
                \n";
        }

        $all_sandwich_ingredients = array('mustard', 'mayonnaise', 'pickles',
                                          'lettuce', 'tomatoes', 'cheese');

        $standard_sandwich_ingredients = array('lettuce', 'tomatoes', 'cheese');

        populateForm('Sandwich', 12, 6.50, 1, $standard_sandwich_ingredients, $all_sandwich_ingredients);
?>
cart.php

Code: Select all

<?php
        session_start();

        /*if this is the first page the user sees, register the session id now*/
        if(!isset($_SESSION['session_id'])){ $_SESSION['session_id'] = session_register(); }

        /* print $_SESSION data*/
        echo "<b>SESSION ARRAY:</b>\n<pre>\n"; print_r($_SESSION); echo "</pre>\n";

        /* print $_POST data*/
        echo "<b>POST ARRAY:</b>\n<pre>\n"; print_r($_POST); echo "</pre>\n";

       /*
        * if the user is sent to this page from submiting an item
        * if the shopping cart is empty, there are no items to 
        * compare with. so, it won't proceed, it will just add
        * it as the first submitted item to the cart. if there
        * are items in the cart, we will assume that the $_POSTed
        * item is not identical to any items in the cart. now,
        * we run through each item in the cart one by one. we
        * first compare the id's of the shopping cart item that
        * is being evaluated and the item that was $_POSTed. if
        * the id's do match, sort their item attributes. then,
        * compare the arrays. if the attributes match, do not add
        * the $_POSTed item into a new index in the cart array,
        * just add the quantity of the $_POSTed item to the
        * existing identical item already in the cart. if
        * the items are not identical, add the $_POSTed item
        * into a new index in the cart array. unset the $_POST
        * array so if the user 'refreshes' the page, it will not
        * add the item again.
        */
        if(isset($_POST)){
                if(!empty($_SESSION['shopping_cart'])){
                        foreach($_SESSION['shopping_cart'] as $cart_item){
                                if($cart_item['id'] == $_POST['id']){
                                        if(sort($_POST['ingredients'], SORT_STRING) === sort($cart_item['ingredients'], SORT_STRING)){
                                                $cart_item['quantity'] += $_POST['quantity'];
                                        }else{ $_SESSION['shopping_cart'][] = $_POST; }
                                }
                        }
                }else{ $_SESSION['shopping_cart'][] = $_POST; }
                foreach($_POST as $key => $val){
                        unset($_POST);
                }
        }
?>
i am still having the same problems as before for both pages. no errors. just nonfunctional code. a little help is needed. that's why i'm here in the first place.

Posted: Wed Apr 11, 2007 3:51 pm
by visitor-Q
i figured it out =)