A Simple Session-Based Shopping Cart

   

The following example is a simple session-based shopping cart that allows users to add or remove items from their cart.

The application, as shown in Figure 2-3, first asks for the user's name and email address. Users are then brought to the main shopping page where they can add or remove items from their cart by modifying the quantity text field for each item and clicking the "Add Items" button.

Figure 2-3. session_cart.php Sign-In Screen

graphics/02fig03.jpg

When the users have added at least one item, they are presented with a "Check Out" button. They can continue shopping or check out. This is shown in Figure 2-4.

Figure 2-4. session_cart.php Shopping Screen

graphics/02fig04.jpg

When users click "Check Out", they are brought to a confirmation page, which displays the contents of their cart. They then have the choice of finishing the order by clicking the "Finish Order" button or going back and adding additional items to their cart by clicking the "Add More Items" button. This is shown in Figure 2-5.

Figure 2-5. session_cart_checkout.php

graphics/02fig05.jpg

Script 2-5 session_cart.php

[View full width]

  1.  <?  2.  session_start();  3.  $inventory = array(  4.      "001" => "Tooth Paste",  5.      "002" => "Facial Tissue",  6.      "003" => "Cotton Swabs",  7.      "004" => "Shampoo",  8.      "005" => "Conditioner",  9.      "006" => "Deodorant" 10.      ); 11.  function populate_cart() { 12.    global $name, $email, $items; 13.    $items = array(0, 0, 0, 0, 0, 0); 14.    session_register("name"); 15.    session_register("email"); 16.    session_register("items"); 17.  } 18.  function get_user_info() { 19.    ?> 20.    <h3>Please provide us with the following before continuing:</h3> 21.    <form action=session_cart.php method=post> 22.    <p>Name: <input type="text" name="name"> 23.    <br>Email: <input type="text" name="email"> 24.    <p><input type="submit" name="infosubmit" value="Submit"> 25.    </form> 26.    <? 27.  } 28.  function shop() { 29.    global $name, $email, $items, $inventory; 30.    ?> 31.    <p>Shop Below: 32.    <form action=session_cart.php method=post><p> 33.    <? 34.    $total = 0; 35.    for($i = 0; $i < sizeof($inventory); $i++) { 36.      ?> 37.      Item ID: <b><?=key($inventory);?></b> Description: <b><?=$inventory[key( graphics/ccc.gif$inventory)]?></b> 38.      Qty: <input type="text" name="items_in[<?=$i?>]" value="<?=$items[$i]?>" size="2"  graphics/ccc.gifmaxlength="2"><br> 39.      <? 40.      next($inventory); 41.      $total = $total + $items[$i]; 42.    } 43.    ?> 44.    <p>You have <?=$total?> items in your cart. 45.    <p><input type="submit" name="additems"> 46.    </form> 47.    <? 48.    if($total > 0) { 49.      ?> 50.      <form action=session_cart_checkout.php> 51.      <input type="submit" name="checkout" value="Check Out!"> 52.      </form> 53.      <? 54.    } 55.  } 56.  /* MAIN */ 57.  if(isset($additems)) { 58.    $items = $items_in; 59.    echo("<h3>Cart Updated!</h3>"); 60.    shop(); 61.  }elseif(!isset($name)) { 62.    get_user_info(); 63.  } elseif(isset($infosubmit)) { 64.    populate_cart(); 65.    shop(); 66.  } else { 67.    shop(); 68.  } 69.  ?> 

Script 2-5. session_cart.php Line-by-Line Explanation

LINE

DESCRIPTION

2

Start the session or continue an existing session.

3 10

Create an array to be used as the items available in the store.

11

Declare a new function populate_cart(). This function sets up the user's shopping cart.

12

Declare some global variables for the user. The $items variable is the items in the user's shopping cart.

13

Zero out the items in the user's shopping cart.

14 16

Register the variables that have been declared with the session.

18 27

Declare a function to get the user info when the user first arrives at the shopping cart. The function asks for the user's name and email.

28

Declare the shop function. This function does the work of adding and removing items from the user's shopping cart.

29

Allow the function to access and modify the $name, $email, $items, and $inventory variables.

32

Create the shopping form.

34

Assign a value of 0 to the $totals variable. This variable is used to show how many items are in the user's cart, which is calculated as the user adds or removes items from the cart.

35

Loop through the items in the $inventory array.

37 38

Display the ID (key) and Description (value) for each item in the store's inventory. At the same time, display how many items are in the user's cart in a text input field. If the user has not added any items into the cart, then all values read as 0, which is the value that was used to initialize the cart.

40

Use the next() function so that the next key is available the next time the loop is executed.

41

Add the current total of items in the user's cart for this loop to the running total of items.

42

Exit from the for loop.

44

Display the current item total to the user.

45

Display an "Add Items" button. It is used to execute the additems clause in the main program.

48 54

If the user has at least one item in the cart, then display the "Check Out" button, which brings the user to the session_cart_checkout.php page.

55

End the function.

56

Begin the main program.

57 59

Check to see if the "Add Items" button has been pushed. Display a message to the user that the cart has been updated after the $items session variable is updated.

58

If the "Add Items" button has been pushed, then replace the session variable $items with the items from the form $items_in. The session variable is not automatically updated when you submit variables from a form. You need to explicitly assign a new value to the session variable.

59

Display a message to the user that the cart has been updated after the $items session variable is updated.

60

Run the shop() function so that the user can add or remove additional items.

61

If the "Add Items" button has not been pushed and the $name variable has not been set, then execute the get_user_info() function, since this is the first time the user has visited the page.

63 65

If none of the above is true, but the $infosubmit variable has been set, then we know the user has just entered the information. Execute the populate_cart() function to initialize the cart.

66 68

If nothing else has happened, just execute the shop function. Since the session variable for $name must be set then, we know the user has entered the information and the cart has been initialized, but no items have been added to the cart.

Script 2-6 session_cart_checkout.php

[View full width]

  1.  <?  2.  session_start();  3.  $inventory = array(  4.      "001" => "Tooth Paste",  5.      "002" => "Facial Tissue",  6.      "003" => "Cotton Swabs",  7.      "004" => "Shampoo",  8.      "005" => "Conditioner",  9.      "006" => "Deodorant" 10.      ); 11.  if(!isset($name)) { 12.    ?> 13.    <h2>Sorry, you need to <a href=session_cart.php>add some items</a> before you can  graphics/ccc.gifcheck out. 14.    <? 15.  }elseif(isset($finish)) { 16.    ?> 17.    <h3>Order Complete!<h3> 18.    <? 19.    // send email confirmation... 20.    // send order to order processing script... 21.    session_unset(); 22.    session_destroy(); 23.  } else { 24.    ?> 25.    <h2>Checkout</h3> 26.    <p>Thank you for shopping with us <?=$name?>, your order is below: 27.    <p><table border=1> 28.    <? 29.    $total = 0; 30.    for($i = 0; $i < sizeof($inventory); $i++) { 31.      ?> 32.      <tr><td>Item ID: <b><?=key($inventory);?></b></td> 33.      <td>Description: <b><?=$inventory[key($inventory)]?></b></td> 34.      <td>Qty: <?=$items[$i]?></td></tr> 35.      <? 36.      next($inventory); 37.      $total = $total + $items[$i]; 38.    } 39.    ?> 40.    <tr><td colspan=3 align=right>Total: <?=$total?></td></tr> 41.    </table> 42.    <p>A confirmation of your order will be sent to the email address: <b><?=$email?></b>, graphics/ccc.gif after you have finished your order. 43.    <p> 44.    <form action=session_cart.php> 45.      <input type="submit" name="continue" value="Add More Items"> 46.    </form> 47.    <form action=session_cart_checkout.php> 48.      <input type="submit" name="finish" value="Finish Order"> 49.    </form> 50.    <? 51.  } 52.  ?> 

Script 2-6. session_cart_checkout.php Line-by-Line Explanation

LINE

DESCRIPTION

2

Start the session or continue an existing session.

3 10

Recreate an array to be used as the items available in the store.

11

Verify that the $name variable is set. If it is, then we know that there is existing session data.

12 14

If the $name variable is not set, then we know that the there is no session data or that it is incomplete. Redirect users back to the shopping cart page so that they can reregister their name and email and add some items to their cart.

15

Check to see if the "Finish Order" has been pushed.

16 20

If the "Finish Order" button has been pushed, then notify the user that the order has been completed. Then do whatever processing is required, such as sending email or sending the order to an order-processing script.

21

Delete all the session variables, since we no longer need the data.

22

Destory the session to remove the session file from the server's disk.

This has the added benefit of not allowing the user to click "Reload" or the "Back" button and produce multiple orders for the same thing.

23

If the order has not been finalized and we know the $name variable has been set in the session, then execute the remainder of the script.

25 41

Display a table using the data from the $inventory array and from the $items in the user's shopping cart. This table is similar to the one used in the session_cart.php script that displays the shopping form, except that the fields are not editable.

42

Display a message providing details to users of what happens when they click the "Finish Order" button.

43 46

Provide a button so that users can go back and edit items in their cart.

47 49

Display the "Finish Order" button so that users can confirm their order.

51

Close out the if/elseif/else statement started on line 11.


   
Top


Advanced PHP for Web Professionals
Advanced PHP for Web Professionals
ISBN: 0130085391
EAN: 2147483647
Year: 2005
Pages: 92

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