Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Databases for Web Applications 3-Tier. Design vs Function vs Content. More SQL. More php. Homework: work on final projects.

Similar presentations


Presentation on theme: "Creating Databases for Web Applications 3-Tier. Design vs Function vs Content. More SQL. More php. Homework: work on final projects."— Presentation transcript:

1 Creating Databases for Web Applications 3-Tier. Design vs Function vs Content. More SQL. More php. Homework: work on final projects

2 Refrain on 3 tier Some divide the html tier into content versus style, with CSS holding the style. This is the interaction tier. –Note: Flash and other languages (Processing, Java, ??) also do more function Middle tier, php, do 'business logic', other function. Information tier, MySQL, holds information! –Serves multiple functions. Implemented (possibly) by different groups in an enterprise.

3 Another tier? or is the 3 tier terminology insufficient Organizations use code and content developed and maintained by others. –Web services –cloud computing –content such as Google maps –??? Extra credit opportunity to report / comment.

4 More SQL Assuming the simple student database (id, sname, department, gpa) Produce a list of departments, with number of students, average, maximum, and minimum gpa Produce a list of departments, number of students with gpa at least 2 (not on academic probation?) Produce a list of departments, number of students on academic probation, limit to the 5 with the most on academic probation

5 Students in clubs Assume the student table (sid, sname, dept, gpa) plus 2 more tables –clubs: clubid, clubname, clubdesc –clubmemberships: mid, sid, clubid Usual technique, similar to tags, ordered items.

6 Generate list of students in club named Hiking SELECT s.sname FROM students as s, clubs as c, clubmemberships as m WHERE c.clubname='Hiking' AND m.clubid=c.clubid AND s.sid=m.sid ALTERNATIVE (may need parentheses) SELECT s.sname FROM students as s JOIN clubmemberships as m ON m.sid = s.sid JOIN clubs as c WHERE clubname='Hiking' ON m.clubid=c.clubid

7 Extra credit (Difficult to check without creating all the tables, but try) Generate list of club names, count of students in clubs, ordered from most to least

8 Left Join Matches rows of two tables using the ON condition, but if something on the LEFT does not have a match, generate a row with null values. Used in the quiz show to find questions that haven't been answered or asked recently. NOTE: can have SELECT within a SELECT

9 Students not in any club SELECT sname FROM SELECT * FROM students as s LEFT JOIN clubmemberships as m ON s.sid=m.sid WHERE m.sid=null

10 php to php One way to acquire and pass information from one php script to another is to use the query string. In the store application, html is produced that has tags with href=makeorder.php?3 for example, where 3 represents a product id.

11 Select product: <?php $query="Select * from catalog"; $result=mysql_db_query($DBname, $query, $link); while ($row=mysql_fetch_array($result)) { print (" <a href=makeorder.php"); print ("?p_id="); print($row['id']); print(">"); print($row['p_name']); print(" "); print(" <img src=\""); $picture=$row['picture']; print("$picture"); print("\" width='200'> "); } print (" ");

12 php to php Alternative to cookies or data passed via query strings are Sessions. The sessions may be passed via the HTTP headers –Extra credit opportunity: research and do posting on php Sessions Access and set using $_SESSION. This, like $_COOKIE, etc. is an associative array: accessed using names not indices. –NOTE: the shopping cart in my store application is stored as a Session variable and is itself an associative array.

13 <?php session_start(); if (!isset($_SESSION["cart"])) { $_SESSION['cart']=array(); $_SESSION['items'] = 0; $_SESSION['totalprice']=0.00; $cart = array(); } else { //print ("cart already started "); $cart = $_SESSION['cart']; } ?>

14 Shopping Cart <? require("displaycartfunction.php"); ?> <?php require("opendbo.php"); ?> Shopping cart <? if (isset($_GET['productid'])) { $p_id = $_GET['productid']; $quantity=$_GET['quantity']; $cart[$p_id] = $quantity; $_SESSION['cart'] = $cart; }

15 displaycart(); ?> Checkout (submit order)! More shopping!

16 displaycart Function stored in file displaycartfunction. Assumes that connection has been made and session started. Makes use of the foreach construction for associative arrays. –Since associative arrays don't use index values 0 to length of array, what is the code to examine each element? Answer: foreach($aa as $key=>$qty) { } assuming $aa is the associative array and $key and $qty are variables used in the loop for the keys and values –Makes use of number_format($totalprice,2) to produce dollars and cents

17 <?php //assumes that opendbo called, and session started when call is made. function displaycart() { global $cart, $DBname, $link, $totalprice; print (" "); print (" Product ID Product Name Quantity Total cost "); $items = 0; //note session variable items not used $totalprice = 0.00; $cart = $_SESSION['cart'];

18 foreach (@$cart as $pid => $qty) { $items += $qty; //print(" the pid is ".$pid. " and the qty is ". $qty); $query="Select * from catalog where id='$pid'"; //print("query is $query"); $result = mysql_db_query($DBname, $query, $link); $item_price = mysql_result($result,0,"cost"); $item_name = mysql_result($result,0,"p_name"); $item_total_price = $item_price * $qty; $totalprice += $item_total_price; $item_total_pricef = number_format($item_total_price,2); print (" $pid $item_name $qty $item_total_pricef "); }

19 $totalpricef = "$". number_format($totalprice,2); print(" TOTALS $items items $totalpricef "); $_SESSION['items']=$items; $_SESSION['totalprice']=$totalprice; } ?>

20 Class work Gather around someone with laptop. Each group find different uses of JOIN (inner join) and LEFT JOIN to explain.

21 Homework Work on final projects.


Download ppt "Creating Databases for Web Applications 3-Tier. Design vs Function vs Content. More SQL. More php. Homework: work on final projects."

Similar presentations


Ads by Google