<html>

<body>
<h1> Shopping Cart</h1>
<br>
<br>

<?php

        $verbose = true; //set to false to get rid of additional print outs

 

        //========================================
        // Class item = represents a product that is a item is in shopping basket
        class item {
                var $code; // code
                var $name; // name
                var $quantity; // quantity
                var $price; // price per item

                function item($code, $name, $quantity, $price) {
                        $this->code = $code;
                        $this->name = $name;
                        $this->quantity = $quantity;
                        $this->price = $price;
                }
        }

        //========================================
        // Class basket = represents shopping basket with the variable $session_basket representing the Array of item instances in basket

        /**
        * shopping basket class
        */
        class basket {

                /**
                * constructor
                */
                function basket() {
                        $this->sessionStart();
                }

                /**
                * start session OR if one already created retrieve shopping_basket
                */
                function sessionStart() {
                        global $session_basket;      //global variable ---array of items in basket

                        //start session or retrieve if already exists with client
                        session_start();

                        if($verbose) //verbose printout --not necessary
                                echo "session id ". session_id() . "<br>";

                        //if previouisly started grab data associated with session_basket
                        if(isset($_SESSION['session_basket']))
                        {
                                $session_basket = $_SESSION['session_basket'];
                                if($verbose) //verbose printout --not necessary
                                        {      echo "retrieved session basket is: ";
                                                print_r($session_basket);
                                                echo"<br>"; }
                        }
                        else
                        {      //if no session_basket initially to empty array
                                $session_basket = Array();

                                //store in SESSION variables
                                $_SESSION['session_basket'] = $session_basket;

                                if($verbose) //verbose printout --not necessary
                                        echo "session basket NEW";
                        }
                }

                

                /**
                * destory session -- call when someone wants to completely CLEAR the cart --get rid of session
                */
                function sessionEnd() {
                        session_unset();
                        session_destroy();
                }


               /**
                *determine the number of elements in basket
                */
                function basketSize() {
                        global $session_basket;

                        // make session if not found
                        if ($session_basket == "") {
                                $this->sessionStart();
                        }

                        if (! is_array($session_basket)) {
                                return 0;
                        }
                        return $i;
                }

                /**
                * register item in session
                * if same code exist in session, modify it.
                */
                function registerItem($code, $name, $quantity, $price) {
                        global $session_basket;

                        // make session if not found
                        if ($session_basket == "") {
                                $this->sessionStart();
                        }

                        // test to see if this product (with id $code) is currently IN basket, if so EDIT IT (update)
                        if (! $this->editItem($code, $name, $quantity, $price)) {
                                $item = new item($code, $name, $quantity, $price); //if NOT in basket CREATE IT
                                $session_basket[] = $item;
                        }

 

                        //Make sure to add updated $session_basket array to the SESSION variables

                        $_SESSION['session_basket'] = $session_basket;
                }

                        

                /**
                * see if product (with product id $code) is in the current $session_basket array
                * if exist, modify it and return true
                * else retrun false
                */
                function editItem($code, $name, $quantity, $price) {
                        global $session_basket;

                        // make session if not found
                        if ($session_basket == "") {
                                $this->sessionStart();
                                return false;
                        }

                        reset($session_basket);
                        while(list($k, $v) = each($session_basket)) { //search in $session_basket
                                if ($session_basket[$k]->code == $code) { //if found matching code (product id)
                                        // Found same code --- upade with new values the item
                                        $session_basket[$k]->name == $name;
                                        $session_basket[$k]->quantity = $quantity;
                                        $session_basket[$k]->price = $price;
                
                                       if($verbose) //verbose printout --not necessary
                                                echo "INSIDE editItem: " . $code . "<br>";

                                        return true; //return true we updated it
                                }
                        }

                        return false; //could not find the product currently in basket
                }

 

                /**
                * delete item from basket ($session_basket array) that has product id of $code and name of $name
                */
                function deleteItem($code, $name) {
                        global $session_basket;

                        // make session if not found         
                        if ($session_basket == "") {
                                $this->sessionStart();
                        }

                        reset($session_basket);
                        while(list($k, $v) = each($session_basket)) { //look through each item in basket
                                if ($session_basket[$k]->code == $code) { //if this item's code matches $code then we found the one to remove
                                        unset($session_basket[$k]); // remove this item from the $session_basket array
                                        return true;
                                }
                        }
                }
}

 

 

// STEP 1: Create or Retrive shopping basket if it already is in session
$basket = new basket();


// STEP 2 YOU will need to ALTER THIS CODE IN THE FOLLOWING WAYS:
// 1) Pass a parameter to the code when invoked telling it if you are "adding" (to basket), "deleting" (from basket), "clearing"
// entire basket.
// 2) Call this parameter "Desired_Action" and test for it. THE CODE BELOW is ONLY the code for adding/updating the basket
// a) Desired_Action = Adding/Updating
// then read in product information passed to this program from a form called "Add to Cart" to create an item and
// then call $basket->registerItem(***) see below
// b) Desired_Action = Deleting
// then read in the product id into variable $code and product name into variable $name of the item you want to
// delete from the basket. You get this information from the form that invokes this method passing. Then
// call $basket->deleteItem($code,$name);
// c) Desired_Action = Clear
// simply call $basket->sessionEnd();

 

 

//FOLLOWING CODE will be removed and replaced by step 2 above you must write. The following code
// creates 2 dummy products and adds them and then creates each time another product with
// a unique id (made unique by setting it to the current time. Because it is unique id, each time you invoke this
// program it will add this new product item to the basket.
// First time the code is invoked it adds 3 product items to the basket. And second or 3rd or Xth time it is
//invoked it will only add the 1 additional unique id product

//Dummy code (you will delete) that adds 2 products statically
$basket->registerItem("asf000000", "implant", "1", "1000000");
$basket->registerItem("abcd000001", "avana implant kits", "10", "15000");

//Dummy code to register a item with product ID associated with time
$uniqueid = time();
$basket->registerItem($uniqueid, "avana implant kits", "10", "15000");

//METHOD 1:print out basket (minimal formatting here you will need MORE)
reset($session_basket);
$i=0;
print("<table border=\"2\"><tr><td>Item#</td><td>Product ID</td><td>Product Name</td> <td> Quantity </td> <td>Price/item</td> <td>Cost</td></tr>\n");
while(list($k, $v) = each($session_basket)) {
$i++;
$item = $session_basket[$k];
echo "<tr>";
echo "<td>" . $i .")</td>" ;
echo " <td>". $item->code . "</td>";
echo " <td>". $item->name . "</td>";
echo " <td>". $item->quantity . "</td>";
echo " <td>$". $item->price . "</td>";
echo " <td>$". ($item->price*$item->quantity) . "</td>";
echo "</tr>";

}

?>

//METHOD 2:print out the basket (minimal formatting here you will need MORE)
<table border=\"2\"><tr><td>Item#</td><td>Product ID</td><td>Product Name</td> <td> Quantity </td> <td>Price/item</td> <td>Cost</td></tr>
<?php
reset($session_basket);
$i=0;
while(list($k, $v) = each($session_basket)) {
$i++;
$item = $session_basket[$k];
?>
<tr>
<td><?php echo $i .")" ; ?>
<td><?php echo $item->code; ?> </td>
<td><?php echo $item->name; ?> </td>
<td><?php echo $item->quantity; ?> </td>
<td><?php echo $item->price; ?> </td>
<td><?php echo ($item->price*$item->quantity); ?> </td>
</tr>

<?php
} //end of loop
?>

 

</table>

<br>
<br>
<br>

done!
</body>
</html>

© Lynne Grewe