Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITM 352 - © PortFunctions - 1 ITM 352 Assignment 1 Application Workshop.

Similar presentations


Presentation on theme: "ITM 352 - © PortFunctions - 1 ITM 352 Assignment 1 Application Workshop."— Presentation transcript:

1 ITM 352 - © PortFunctions - 1 ITM 352 Assignment 1 Application Workshop

2 ITM 352 - © PortFunctions - 2 Announcements r Assignment #1 due Monday at 11:59pm m Must upload to class website m Don’t forget to test it! m Put “your own touch” on this application r Do what you think is best, not just what is easy do implement r Think about what works for your particular application m Don’t forget to go through the quality checklist!

3 ITM 352 - © PortFunctions - 3 Modularity and Assignment #1 An introduction to architecture options

4 ITM 352 - © PortFunctions - 4 Assignment #1 single-file Structure Option Display Products and Invoice Page Selected quantities r Discussion a. What is good about this approach? b. What are the shortcomings of this approach? c. Under what conditions would you use this approach? d. What functions might you define here?

5 ITM 352 - © PortFunctions - 5 Assignment #1 two-file Structure Option Display Products Page Display Invoice Page Selected products and quantities r Discussion a. What is good about this approach? b. What are the shortcomings of this approach? c. Under what conditions would you use this approach? d. What functions might you define here?

6 ITM 352 - © PortFunctions - 6 Assignment #1 three-file Structure Option Products Info Display Products Page Display Invoice Page includes Selected quantities r Discussion a. What is good about this approach? b. What are the shortcomings of this approach? c. Under what conditions would you use this approach? d. What functions might you define here?

7 ITM 352 - © PortFunctions - 7 Other Options r Validate quantities on invoice page r Duplicate products info on products and invoice pages r Include files rather the re-directing

8 ITM 352 - © PortFunctions - 8 Modularity r Modularity is creating functional “parts” that work together to perform tasks m Each part is “encapsulated” or “hidden” from other parts in some way by an “interface” m Some ways to encapsulate parts: r Different files r Functions r Objects m Discussion: what is the interface for each of the above?

9 ITM 352 - © PortFunctions - 9 Basic Architecture Advice r Always try to eliminate data redundancy by modularizing the data r One file, one concern – modularize tightly related activities and functions r Balance complexity of interaction of parts versus complexity of the parts r Modularize shared functionality (see functional modularity next)

10 ITM 352 - © PortFunctions - 10 Exercise <?php $prod1 = array('image' => "HTC.jpg", 'brand' => "HTC Phones", 'price' => 40); $prod2 = array('image' => "iphone-3gs.jpg", 'brand' => "Apple Phones", 'price' => 75); $products = array($prod1, $prod2); ?> ITM352 Used Smartphone Store <?php if (!isset($_POST['quantity'])) : ?> Welcome to the ITM352 Used Smartphone Mart " method="post"> <?php display_products($products); ?> <?php else : $errors = array(); validate_quantities($_POST['quantity']); if (!empty($errors)) { display_products($products); } else { display_products($products, $_POST['quantity']); } endif; ?> <?php function validate_quantities($quantities) { global $errors; foreach ($quantities as $index => $quantity) { if ($quantity < 0) @$errors['quantity'][$index].= 'Quantity must be non-negative '; if (!ctype_digit($quantity)) @$errors['quantity'][$index].= 'Quantity must be an integer '; } function display_products($products_to_display, $quantities = array()) { global $errors; var_dump($errors); ?> Product Brand Price (each) Quantity <?php for ($i = 0; $i < count($products_to_display); $i++) { if (empty($quantities)) { $qty = isset($_POST['quantity'][$i])?$_POST['quantity'][$i] : 0; $qty_str = " "; if (isset($errors['quantity'][$i])) $qty_str.= ' '. $errors['quantity'][$i]. ' '; } else { $qty_str = $quantities[$i]; } printf(' <img alt="Small" id="lightboxImage" style="width: 119px; height: 88px;" src="http://dport96.github.io/ITM352/morea/090.flow-control-II/%s" height="300" width="300"> %s $%.2f '. $qty_str. ' ', $products_to_display[$i]['image'], $products_to_display[$i]['brand'], $products_to_display[$i]['price']); } ?> <?php } ?> Copy the code to the left into NetBeans. It is an example of the single file architecture 1.Adapt it to a two-file architecture 2.Adapt it to a three-file architecture 3.Can you make better use of include/require ?

11 ITM 352 - © PortFunctions - 11 HTML and PHP

12 ITM 352 - © PortFunctions - 12 Switching Between PHP and HTML  Recall #1: Any text outside the tags will just be passed to the browser as plain text to the browser and thus interpreted as HTML. r Recall #2: After PHP executes it outputs plain text and thus interpreted as HTML by the browser  You can mix HTML and PHP code arbitrarily by using multiple tags r Just remember that the ultimate output is text that will be interpreted at HTML so construct things that will always output the HTML you want

13 ITM 352 - © PortFunctions - 13 PHP in HTML ’ > Or a shortcut for the same: ’ > r Remember that HTML does funny things with spaces and tabs so note the use quotes when you need to ensure the exact strings  if $str = “the name” then the above code would not work without the quotes r HTML can use either single or double quotes

14 ITM 352 - © PortFunctions - 14 HTML in PHP echo “ ”; Or the same: echo ‘ ’; r Note the different use of single and double quotes

15 ITM 352 - © PortFunctions - 15 Advice r If you need to write a lot of static HTML then you should turn off PHP and write the HTML you need and turn on PHP in the parts that need it (i.e. are dynamic) r If you need to create a bit of dynamic HTML then just echo (or print) the HTML you need inside the PHP code r Consider using a HEREDOC string when you need to use a lot of variables and quotes

16 ITM 352 - © PortFunctions - 16 Dealing With PHP Warnings

17 ITM 352 - © PortFunctions - 17 PHP Warnings r Warnings indicate potential errors or problems r Common warnings m Undefined variable m Undefined index m No file-stream r Address warnings by m Avoiding problem m Suppress warning r Good choice when it is certain that no consequences or side effects will occur m Bad: hide warnings with black text on black background!

18 ITM 352 - © PortFunctions - 18 Exercise a. How would you fix the inevitable warning for the above code? b. How would you suppress a warning for the above? c. When is it acceptable to suppress a warning rather than avoid it? if( $_POST['select_box'] == '' ) { echo 'nothing selected!'; }

19 ITM 352 - © PortFunctions - 19 Exercise: Passing Data With Redirect <?php if (isset($_POST['doit']) ) header ("Location: hidden1.php"); else { ?> "> <? } ?> 1.How can you pass the form data from a self-processing page? 2.When might such a situation arise?


Download ppt "ITM 352 - © PortFunctions - 1 ITM 352 Assignment 1 Application Workshop."

Similar presentations


Ads by Google