IT360 Lab 9: PHP Intro


DUE: March 24, 2014, 1159 (paper copy due BEFORE start of lab the next day)


In this lab, you will start creating a web front for an Online Store application, so customers can eventually buy products online: you should display a list of products that can be bought, process the order and display a confirmation message. In later labs, you will connect the web-based interface to the database.  We will use files and hard-coded product information for now.


Set-Up - Windows Server




Important – the following will save much pain later.  You should also do this on your PC in your room: Open up the File Explorer (one way is to go to the Start menu and click Computer).  Click on the W: drive. Then select “Organize -> Folder and Search Options.” Select the View tab.  Look for an option titled “Hide extensions for known file types.”  Make sure this option is NOT checked, then click on the button at top that says “Apply to All Folders.”





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


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. At a minimum, each file should have your name, alpha and a short description on top, and each function should have a short description.

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),

o   Methods: display

·         Product (used to insert, delete, and display product information),

o   Methods: getAllProductsAsHTML

·         Order (used to create and process an order).

o   Methods: processOrder, insertOrderToFile

You are going to create several scripts:

·         getOrder.php: an “Order” page, where the customer can select the products he/she wants to buy.

·         receiveOrder.php: the script to process the order: compute the total price for the order, store the order data into a file, output a confirmation message to the user.

·         (extra credit) displayOrders.php: for extra credit, create a script to output all the orders, so a manager can see what was ordered.



Part 1: Page  class (5%)

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 (see the first PHP lecture links).





//create a page object and set the title

$page = new Page("Online Store");

//set the page content

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

//display page




Part 2: Product class (10%)

Since your online store works with products, it makes sense to create a Product class (the file name should be Eventually, this class will have methods to create, read, update, and delete products. For now, just create a static method getAllProductsAsHTML(). This method will return a string - the HTML code to display all products as a table with input text fields for quantity to be ordered. To create a static method, you place the keyword static before the function  definition (so you’ll have public static function getAllProductsAsHTML(){….}) 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. Below is a sample table screen shot.



To test your Product, create a new file getOrder.php with the following content (feel free to modify it to match your needs). Run your script by typing in the address bar of the browser.






//create a page object

$page = new Page("Order Products");

//set the page content

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

//display page




Part 3: GetOrder  (10%)

Modify your getOrder.php script to display a form that allows the user to order products from your store.  The code is similar with the one you already have, but you have to put the table into a form, and add a submit button. The “action” for the form should be “receiveOrder.php”.


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



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


Part 4: Order class (5%)

Create an Order class (the file name should be Later, the class will contain information about all products ordered, but for this lab, only two products can be ordered. Your Order class should store as attribute(s) the name and quantity for products ordered. Create a constructor for the class, and setter (__set) and getter (__get)  methods.


Part 5: Process Order (60%)

To process the form, you need to create the script mentioned in the action attribute of the form tag called receiveOrder.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[‘quantity_1’] will hold the value assigned to input text named "quantity_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). To accomplish that, you should add a processOrder()  method to the Order class that does all the processing, and call that method in your receiveOrder.php script (after you properly instantiated an Order object of course).


Task 2: What if the person does not order all 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: Modify your code to compute the total quantity and total amount for the order as shown below. Assume some prices for your products (tee price is $15 and patch  price is $10 in my example). Use the “number_format” (see 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 4: Create the insertOrderToFile() method in the 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 the follwing, separated by some special character, such as tab:

-T-shirts quantity

-patches quantity

-shipping address (if you added one as extra credit)

-total amount (computed)


Add code to your processOrder() method in the Order class to call the insertOrderToFile during processing, and modify receiveOrder.php to display an appropriate message in the order confirmation screen. If for any reason the data cannot be saved, an error message should be displayed. This is a sample output if there was a problem with the file:


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 receiveOrder.php to retrieve and respectively save and display the shipping address in the confirmation.      


Task 6 (EXTRA CREDIT): Create a script displayOrders.php to 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. Most of the work of retrieving data from the file should be done in a method in Order class.



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 (electronic copy due before 2359 on March 24, 2014. Paper copy due before start of lab on March 25, 2014):


  1. Upload all files from Lab8 to the Lab 8 assignment on the blackboard.


  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 receiveOrder.php is executed.
  3. A hard copy of each file written for this lab. Remember to comment your code! At a minimum, each file should have your name and a short description on top, and each function should have a short description. Print double-sided, or 4 pages per page to save some paper.