CS637 Homework 4 Arrays, pizza1 Solution, session variables

Due Wed., Apr. 12 in class, on paper

1 Arrays. 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. Analysis of the pizza1 solution

a. Find the model API by searching (grep on Linux/Mac or find on Windows, or use Netbeans) for “function” across all the files in model. Show the output of your command(s)/Netbeans search.
b. By MVC rules, calls to these functions must all come from the controllers, that is, not from any view file. A partial test of this is to search all files for “get_” and see if any hits are in view files (non-index files in pizza or admin/* or view). Report on what you find.
c. By MVC rules, no HTML should occur in controller files (index files in pizza or admin/*, ones controlled by $action). A test of this is to search all index files for “<” and see if hits are HTML. In Netbeans, use Find for the project, fill in “<” and a file name pattern of “index.php”. Report on this search. Note that some index.php files are “navigational”, not really controllers, and just contain links to other parts of the website.

3. Managing State using hidden parameters. We saw this in action in the TicTacToe application, where the whole tic-tac-toe grid state was sent back to the server on each move.  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 follows then the link to order another pizza, the room number is pre-selected for them in the order form.  How does the controller of the order form know the room number?  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.

4. Managing State using Session Variables. (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.

5. Passing Arrays into Functions. Consider problem 4 on pg. 413. Note how passing arrays into a function works differently from Java. Both languages use “call by value”, 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”.

    a. Set up:

Compose file cart1.php with code on pg. 393, at least cart_add_item function, with changes as suggested in problem 4 of pg. 413:

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

Compose file test1.php that includes cart1.php: code like that at bottom of pg. 393 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, 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.