10.9 Creating a Shopping Cart Using Sessions and PHP


The next section of this chapter deals with storing variables and data, but before we go into that, I think the sessions part of this chapter should be culminated with a script. Therefore, I've written a small shopping cart script that contains all the necessary features (add items, take away items, view items). Please note that this script does not contain a checkout portion because that is off the topic.

site_lib.inc:

 <?php // // site_lib.inc --> //   Contains the LoadProducts() function. // // Global Array with all the products, // we fill up $master_products_list with // the LoadProducts() function. $master_products_list = array(); // // void LoadProducts (void) //   Load all of the products for this shopping cart into the global //   $master_products_list array. // function LoadProducts() {     global $master_products_list;     $filename = 'products.txt';     $fp = @fopen($filename, "r")         or die("Cannot open $filename");     // Acquire a shared lock, precautionary,     // would be important if we were also writing to     // the file     @flock($fp, 1)         or die("Cannot acquire a shared lock on $filename");     while ($line = fgets($fp, 1024)) {         list($id, $name, $desc, $price) = explode('', $line);         $id = trim($id); // Cut away the fat         $master_products_list[$id] = array("name" =>  $name,                                            "desc" =>  $desc,                                            "price" => $price);     }     @fclose($fp)         or die("Cannot close $filename"); } ?> 

products.txt:

 2kd230  Bicycle  The coolest bicycle in the world  23.83 dksk21  Sony Playstation  Video game excitement  123.00 

cart.php:

 <?php // // cart.php:  The main file // require 'site_lib.inc'; session_register('cart'); // Register our session // Initialize the Cart if it is not already initialized if (!isset($cart[num_items])) {     $cart = array("num_items" => 0,                   "products"  => array()); } // From site_lib.inc, Loads the $master_products_list array LoadProducts(); ?> <html> <head>     <title>Sterling's Toy Shop</title> </head> <body> <h1>Welcome to Sterling's Toy Shop</h1> <?php if ($cart[num_items]) {  // If there is something to show ?> <h2>Items Currently in your shopping cart</h2> <br> <table border="2" cellpadding="5" cellspacing="2"> <tr>     <th>         Product Name     </th>     <th>         Short Description     </th>     <th>         Price     </th>     <th>         Quantity     </th>     <th>         &nbsp;     </th> </tr> <?php     // Loop through the products     foreach ($cart[products] as $i => $product) {         $product_id = $product[0];         $quantity   = $product[1];         $total += $quantity *                   (double)$master_products_list[$product_id][price]; ?> <tr>     <td>         <?php echo $master_products_list[$product_id][name]; ?>     </td>     <td>         <?php echo $master_products_list[$product_id][desc]; ?>     </td>     <td>         <?php echo $master_products_list[$product_id][price]; ?>     </td>     <td>         <form action="change_quant.php" method="GET">         <input type="hidden" name="id" value="<?php echo $i; ?>">         <input type="text" size="3" name="quantity"                 value="<?php echo $quantity; ?>">     </td>     <td>         <input type="submit" value="Update Quantity">         </form>     </td> </tr> <?php     } ?> <tr>     <td colspan="2">        <b>Total: </b>     </td>     <td colspan="2">         $<?php echo $total; ?>     </td> </tr> </table> <br> <br> <?php } ?> <h2>Toys for sale at Sterling's toy shop</h2> <br> <i>     We offer the following toys for sale: </i> <br> <table border="2" cellpadding="5" cellspacing="2"> <tr>     <th>         Product Name     </th>     <th>         Product Description     </th>     <th>         Price     </th>     <th>         &nbsp;     </th> </tr> <?php     // Show all of the products     foreach ($master_products_list as $product_id => $item) { ?> <tr>     <td>         <?php echo $item[name]; ?>     </td>     <td>         <?php echo $item[desc]; ?>     </td>     <td>         $<?php echo $item[price]; ?>     </td>     <td>         <a href="add_item.php?id=<?php echo $product_id; ?>">             Add This item to your shopping cart         </a>     </td> </tr> <?php     } ?> </table> 

add_item.php:

 <?php // // add_item.php: //  Add an item to the shopping cart. // require 'site_lib.inc'; // LoadProducts() LoadProducts(); // Load products in $master_products_list // Make $curr_product global $curr_product = array(); // Loop through all the products and pull up the product // that we are interested in foreach ($master_products_list as $prod_id => $product) {     if (trim($prod_id) == trim($id)) {         $curr_product = $product;     } } // Register our session session_register('cart'); if ($ordered) {  // If they have chosen the product     array_push($cart[products], array(trim($id), $quantity));     $cart[num_items] += $quantity; } ?> <html> <head>     <title>     <?php if ($ordered) {  ?>         Added <?php echo $curr_product[name]; ?> to your shopping cart     <?php } else {  ?>         Add <?php echo $curr_product[name]; ?> to your shopping cart     <?php } ?>     </title> </head> <body> <?php if ($ordered) {  ?>     <h1><?php echo $curr_product[name]; ?>         Was successfully added to your shopping cart</h1>     Go <a href="cart.php">back</a> and continue shopping. <?php }  else {  ?>     <h1>Add <?php $curr_product[name]; ?> to your shopping cart</h1>     <form action="<?php echo $PHP_SELF; ?>" method="GET">     Product Name: <?php echo $curr_product[name]; ?>     <br>     Product Description: <?php echo $curr_product[desc]; ?>     <br>     Product Price: $<?php echo $curr_product[price]; ?>     <br>     Product Quantity: <input type="text" size="7" name="quantity">     <input type="hidden" name="id" value="<?php echo $id; ?>">     <input type="hidden" name="ordered" value="1">     <input type="submit" value="Add to cart">     </form> <?php } ?> </body> </html> 

change_quant.php:

 <?php // // change_quant.php: //   Change the quantity of an item in the shopping cart. // session_register('cart'); // register the session // Typecast to int, making sure we access the // right element below $i = (int)$id; // Save the old number of products for display // and arithmetic $old_num = $cart[products][$i][1]; if ($quantity) {     $cart[products][$i][1] = $quantity; //change the quantity } else {     unset($cart[products][$i]); // Send the product into oblivion } // Update the number of items $cart[num_items] = ($old_num > $quantity) ?                    $cart[num_items] - ($old_num-$quantity) :                    $cart[num_items] + ($quantity-$old_num); ?> <html> <head>     <title>         Quantity Changed     </title> </head> <body>     <h1> Quantity Changed from <?php echo $old_num; ?> to          <?php echo $quantity; ?></h1>     Go <a href="cart.php">back</a> and shop some more. </body> </html> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net