CS637 Homework 4 Arrays, pizza1 Solution, session variables, intro objects

Due Wed., Apr. 18 in class, on paper. Note that Mon., Apr. 16 is a holiday.

1 Arrays, pizza1 solution. Consider the variable $room_preparing_orders in pizza1/pizza/index.php which holds an array of rows, where each row is represented by a associative array of column name-values from one row of the pizza_orders table augmented by an array of topping names (see the model function code).
a.       Draw a picture of this data structure like those on pg. 337. Assume two rows with two toppings and three toppings, respectively.
b.      Write a fragment of PHP code to count the toppings across all the rows (maybe more than two) held in $room_preparing_orders.

2. 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 room number on the order form. This room number then shows up on next displayed page, along with a table of orders for that room (no hidden parameter yet needed).  If the user then follows the link to order another pizza, the room number is pre-selected for them in the order form.  How does the controller know the room number in this new request cycle?  Similarly, the user could acknowledge receipt of pizzas, causing another request-response cycle, yet again the controller knows the room number when redoing the page.  Explain how the room number information is transmitted from one request-response cycle to the next.

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

4. 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”. Consider problem 4 on pg. 413/415, specifically as follow:

    a. Set up:

Create a directory, say cartapp, for this problem. Compose file cart1.php with code on pg. 393/395, or at least the cart_add_item function, with changes as suggested in problem 4 of pg. 413/415:

function cart_add_item($cart, $name, $cost, $quantity)…

Compose file test1.php that includes cart1.php: code like that at bottom of pg. 393/395 that calls these functions, specifically the following, plus setup code:

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

Run the code (php test1.php) and explain why the printed cart is empty.

b.      Fix the problem by returning the changed cart in cart2.php’s cart_add_item and using it in the test code:

Specifically, compose test2,php, which includes cart2.php, with code:

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

Explain what happens.

c.   Finally, compose file  cart3.php that uses a var parameter for cart (i.e., passes cart by reference) as in for cart_add_item as shown on pg. 393/395, and go back to previous test code, except for the include of cart3.php.

Compose file test3.php that includes cart3.php, but otherwise looks like test1.php:

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

Explain what happens and compare to Java.

5. 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. Show the output of your program in your paper, along with the text of list_products.php and product_db.php.