CS637 Homework 4 pizza1, session variables, objects, intro web services

Due Mon., Nov. 23 on Gradescope.

1. Managing State using hidden parameters, pizza1 solution. We saw hidden variables in action in the TicTacToe example in homework 2, where the whole tic-tac-toe grid state was sent back to the server on each move, then back to the client again in an updated hidden variable in the returned HTML.  Now consider the pizza1 solution. When the user of the pizza1 solution makes the first pizza order, he/she specifies a username on the order form (or earlier, on the student welcome page). This username choice shows up on next displayed page (the student welcome page), along with a table of orders for that user.  If the user then follows the link to order another pizza, the username is pre-selected for them in the order form.  How does the controller know the selected user in this new request cycle?  Similarly, the user could acknowledge receipt of pizzas, causing another request-response cycle, yet again the controller knows the selected user when redoing the page.  Explain how the selected-user information is transmitted from one request-response cycle to the next. Note that pizza1 does not use session variables.

2. Managing State using Session Variables, pizza1 solution. (Chapter 12) Change a few lines of the pizza1 solution to use a session variable for the selected user, and remove the mechanism you described in problem 1. Explain what lines you changed in what files or provide diffs.

3. Passing Arrays into Functions. As explained in slide 13 of the Chapter13 slide set, we need to pass arrays by reference if we want the function to change the caller's array. Note how passing arrays into a function works differently from Java. Both languages use “call by value” by default, but the value is the whole array in PHP and just the array reference in Java, unless (in PHP) you use & on the array variable in the function definition to change to “pass by reference”.

Create a directory, say cartapp, for this problem. Copy the supplied cart.php.txt to it and rename it to cart.php. Note that this code is similar but not identical to the code on pg. 393 (2nd ed)/395(3rd ed). 

a. Compose a command-line PHP file test1.php that includes cart.php: code like that at bottom of pg. 393/395 that calls these functions, specifically the following, plus setup code: 

$cart = array();
add_item($cart, ‘Flute’, 149.95, 1);
print_r($cart);

Run the code (php test1.php) and see that the cart has the expected item. Report success or problems.

b.  Change cart.php's add_item by removing the & on the $cart parameter, and call the result cart2.php. Compose test2,php, which includes cart2.php, with code:

$cart = array();
$cart = add_item($cart, ‘Flute’, 149.95, 1);
print_r($cart);

Report on the output. Explain what happens and compare to Java.

4. Intro to PHP objects. Set up a directory with copies of Product.php, Category.php, product_db.php, category_db.php and database.php from the sample app book_apps/ch14_guitar_shop. Write a command line program list_products.php that simply prints a list of product code and list price of all the products.  Specifically, do the following:

a. Add a method getAllProducts to product_db.php to return an array of product objects for all the products, following the code for getProductsByCategory (pg 437/439) but going through all the products. Note that you need to find the $category specific for a product here. This can be done by calling getProduct or using some of its code. Alternatively, implement getAllProducts by finding all the categories and then calling getProductsByCategory for each, and combining the results.

b. Write the top-level program list_products.php that sets up the database connection the same way that index.php (shown on pg 441/443) does it, gets the array of all product objects by calling the method you developed in part a., then loops through them printing out the required information (product codes and list prices). Show the output of your program in your paper, along with the text of list_products.php and product_db.php.

5. Intro to Web Services

a. Download and unzip the provided example ch05_gs_client.zip and ch05_gs_server.zip into your local XAMPP webserver htdocs/cs637/username directory. This will give you two projects at htdocs/cs637/username/ch05_gs_client and htdocs/cs637/username/ch05_gs_server. If necessary, load/reload the needed my_guitar_shop1 database using htdocs/cs637/username/ch05_gs_server/database/my_guitar_shop1.sql. Give the URL you used to browse to the home page of the client.

b. Use the browser to obtain the following in JSON: Give the URLs you used and the JSON. For example, to find all the guitars, you would use a GET to URL http://localhost/cs637/username/ch05_gs_server/api/categories/Guitars/products, or guitars instead of Guitars, because mysql uses caseless compare for strings (this can be changed by configuration, but caseless compare is the default).

 i. All the categories

ii. All the basses

iii. All the details on product 3, which is a guitar.

Note: if  you try product 2, the request will fail if you have the full database loaded because there's a bad UTF-8 character in its database record in the long description for product 2. It will work OK for product 2 using the simpler createdb.sql in the database directory of ch05_gs_server. JSON is very particular on handling proper UTF-8.

c. On pe07, unzip the same zips in your /var/www/html/cs637/username/ directory. Use ch05_gs_server/database/createdb.sql (and perhaps dropdb.sql) to load/reload your mysql database on pe07. Find the results of b. using curl and show your commands and output. Also show creation of a new product using JSON in file ch05_gs/ch05_gs_server/product.json, and try it again to see the duplicate product code cause a failure. Also try using brokenproduct.json to create a new product. If you have curl on your development machine (Mac or Linux, or recent Windows 10), it would be good to try some of these there too.

d. Write a small command-line PHP program list_product_ws.php that does the same job as as list_products.php of problem 4. In this case you will need to combine results from the various categories. You may use web_services.php from ch05_gs_client if you wish, but if so, don't edit it. Show the output of your program in your paper, along with the text of list_products_ws.php and any helper files other than web_services.php.