IT360 Lab 6: PHP Intro

 

DUE: March  5, 2010, BEFORE start of lab

In the previous labs, you created a database for an Online Store. In this lab, you will start creating a web front to Online Store application, so customers can eventually buy products online. In later labs, you will connect the web-based interface to the database. 

 

Set-Up 

 
  • We will use the cs-websrvr web server. If you have not done so already, map the W drive to \\cs-websrvr.cs.usna.edu\www.mXXXXXX.it360.cs.usna.edu$ where XXXXXX is your alpha. Use your USNA user name and password. The easiest and fastest way for you to map your web shares is to type “net use w: \\cs-websrvr\www.mXXXXXX.it360.cs.usna.edu$ /persistent:yes” into the command prompt.  You may also use the connection wizard. 

  • Create a directory called “Lab6” on your W drive. All the files you create in this lab should be stored in the Lab6 directory. To run a script called yourfile.php stored in your Lab6 directory on the W drive, type http://www.mXXXXXX.it360.cs.usna.edu/Lab6/yourfile.php in the address bar of your browser (replace XXXXXX with your alpha).

 

  • For all the Web-related labs in IT360 you have to use a plain-text editor such as Notepad++, Crimson editor or PHP Designer 2005.

 

References

You will need a reference book, or online PHP tutorials and references for this lab.

  • The recommended textbook should be very useful; I recommend you have it with you for all PHP related labs: PHP and MySQL Web Development by Luke Welling and Laura Thomson
  • A good online PHP manual can be found at http://us2.php.net/manual/en/index.php.
  • A good online PHP functions reference can be found at http://www.php.net/quickref.php
  • A list of frequent PHP error messages and how to fix the errors is provided on the course website.

 

General Requirements

 

1. You need to use objected-oriented approach when you write your PHP scripts.

2. Make sure the code you write is well documented.

3. While you do not have to validate the XHTML code generated by your PHP scripts, you should try to write/generate well-formed XHTML.

 

General Description

For this lab, you will create and then use three classes: Page (all the scripts that output a page to the browser will use this class), Product (used to insert, delete, and display product information), and Order (used to create and process an order). You are going to create an “Order” page, where the customer can select the products he/she wants to buy. You will also create the script(s) to process the order: output a confirmation message to the user, compute the total price for the order, store the order data into a file. For extra credit, you can also create a script to output all the orders, so a manager can see what was ordered.

 

Part 1: Create Page (5 points)

  • We discussed in class about using a Page class for all pages that you create for your website, and I have provided on the website the source of the Page that I used. Download and modify the page.inc.php from the website to create your version of the Page class – the styles, header and footer information should be appropriate for an online store.
  • To test your Page, create a new file testPage.php with the following content (feel free to modify it to match your needs). Run your script by typing www.mXXXXXX.it360.cs.usna.edu/Lab6/testPage.php in the address bar of the browser.

 

<?php

require_once('page.inc.php');

 

//create a page object

$page = new Page();

//set the page content

$page->title = “Online Store”;

$page->content = “<h1> This is hello world, PHP style! My Page works</h1>”;

//display page

$page->display();

?>

 

Part 2: Create Product (10 points)

Since your online store works with products, it makes sense to create a Product class (the file name should be product.inc.php). Eventually, this class will have methods to create, read, update, and delete products. For now, just create a static method displayAllProductsForSale(). This method will return a string - the HTML code to display all products as a table with an input field for quantity to be ordered. To create a static method, you place the keyword static before the function  definition (so you’ll have static function displayAllProductsForSale(){….}) Eventually, the products will come from the  database, but for now, you can hard-code the product information. For the HTML code that produces a table, you can look at the source of the Sample form provided on the course website. The table produced by this method should look like this:

 

 

To test your Product, create a new file testProduct.php with the following content (feel free to modify it to match your needs). Run your script by typing www.mXXXXXX.it360.cs.usna.edu/Lab6/testPage.php in the address bar of the browser.

 

<?php

require_once('page.inc.php');

require_once(product.inc.php’);

 

//create a page object

$page = new Page();

//set the page content

$page->title = “Online Store”;

$page->content = Product::displayAllProductsForSale();

//display page

$page->display();

?>

 

Part 3: Create Order (5 points)

Create an Order class (the file name should be order.inc.php). Later, the class will contain information about all products ordered, but for this lab, only two products can be ordered. In your Order class create public attributes for name of product 1, quantity for product 1, name of product 2, quantity for product 2.

 

Part 4: GetOrder  (10  points)

 

Create a php script getOrder.php that creates a form to allow the user to order products from your store.  The code for getOrder.php should be similar with testProduct.php, but you have to put the table into a form, and add a submit button. The “action” for the form should be “processOrder.php”.

 

A sample order form is provided on the Course website, but that is an  HTML form. You have to generate similar HTML code your php script, using the Page  class and Product class.

 

 

Test your script by running it from the browser. Since processOrder.php does not exist yet, you should get an error when clicking the submit button.

 

Part 5: Process Order (70 points)

To process the form, you need to create the script mentioned in the action attribute of the form tag called processOrder.php. 

 

The whole point of using the order form is to collect the customer order (and eventually interact with the MySQL database). Getting what the customer typed in is very easy. Simply use the name of the variables from the order page and package them as $_POST[varname] in the PHP script (if you used the POST method to transmit the request to the web server).  Thus $_POST[‘product_1’] will hold the value assigned to product_1 in the order form. 

 

Task 1: Create the php script so your output is similar to the screen below (assuming the user types in ‘2’ as the quantity for each of the items). You should add a displayConfirmation()  method to the Order class and call that method in your processOrder.php script (after you properly instantiated an Order object of course).

 

Task 2: What if the person does not order any of the three items?  What gets displayed?  Modify your order confirmation code so that will only print the items actually ordered.  For example, if you only order 1 patch then you should see the results below. 

 

Task 3: Add 2 attributes to the Order class to store the prices of the products (tee price is 15 and patch  price is 10).

 

Task 4: Modify your code to compute the total quantity and total amount for the order as shown below. Use the “number_format” (see http://www.php.net/quickref.php) function to get the total amount to always print two decimal places.  While you are at it, add in a 5% sales tax for Uncle Sam.

 

Task 5 - EXTRA CREDIT (Add shipping address): For realism sake go ahead and round out your order form to include a shipping address. Modify both the getOrder.php and processOrder.php to retrieve and display the shipping address.  Here are my two versions of the forms.

                    

 

Files

 

Task 6: Add code to your processOrder.php file and Order class to save order data into a file called “orders.txt”. The file “orders.txt” will contain data about all orders. One option for storing the orders is to have one line for each order, with fields separated by some special character, such as tab. For example, each line should contain:

-shirts quantity

-patches quantity

-shipping address (if you did the extra credit Task 5)

-total amount (computed)

separated by some special character, such as tab.

 

Display appropriate message in order confirmation screen. If for any reason the data cannot be saved, an error message should be displayed. This is a sample output if the order was successfully saved.

 

 

Task 7 (extra credit): Read back the data from saved orders file and display all orders from Online Store application. Format the output, so items which were not ordered (0 quantity) are not displayed. This is a sample output:

Online Mids Store – All Orders

2 Tee Shirts
3 Patches
Total Amount: $63.00
Shipping Address: 420 Web Foot Ln, Stevensville MD 21666

 

5 Tee Shirts
Total Amount: $78.75
Shipping Address: 1003 Constitution Av, Washington DC 34012

 

 

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


At this point you should be feeling pretty confident in your ability to generate and process forms using PHP.  Your experience with C++, Java, and Perl makes this relatively easy to do. With a good syntax reference (plenty on the web but a good reference book is the best) you should be able to write server-side scripts.  File I/O, classes, looping constructs, single and multidimensional arrays, string manipulation, and many of the topics covered in IC210, IC211, and IT350 are all featured in PHP. We could go on for weeks just learning and mastering PHP.  The bottom line is given your programming experience you should be able to use PHP to the maximum extent possible by simply using a reference manual to figure out the syntax and semantics of the language.

 

Turn in (due before start of lab on March 5, 2010):

Electronic:

  1. Upload all files from Lab6 to the Lab 6 assignment on the blackboard.

Hard-copies:

  1. The completed assignment coversheet. Your comments will help us improve the course.
  2. A print-screen of the web browser after getOrder.php, and respectively processOrder.php is executed.
  3. A hard copy of each file written for this lab.