Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Evans CS200: Computer Science University of Virginia Computer Science Class 32: Vocational Skills (How to Build.

Similar presentations


Presentation on theme: "David Evans CS200: Computer Science University of Virginia Computer Science Class 32: Vocational Skills (How to Build."— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/~evans CS200: Computer Science University of Virginia Computer Science Class 32: Vocational Skills (How to Build a Dynamic Web Site)

2 10 April 2002CS 200 Spring 20022 Menu Building a Dynamic Web Site SQL PHP HTML Scheme Job listings at dice.com : 1417 ~$40K 60~$60K 4229 $80-250K 1

3 10 April 2002CS 200 Spring 20023 HyperText Transfer Protocol Client (Browser) GET /cs200/index.html HTTP/1.0 … Contents of file Server HTML – hypertext markup language Way of describing hypertext documents

4 10 April 2002CS 200 Spring 20024 HTML Language for controlling presentation of web pages Uses formatting tags enclosed between Not a powerful language – no way to make procedures or jump

5 10 April 2002CS 200 Spring 20025 HTML Grammar Excerpt Document ::= Header Body Header ::= HeadElements HeadElements ::= HeadElement HeadElements HeadElements ::= HeadElement ::= Element Body ::= Elements Elements ::= Element Elements Elements ::= Element ::= Element Make the inner element a paragraph. Element ::= Element Center the element horizontally on the page. Element ::= Element Display the element in bold. Element ::= Text

6 10 April 2002CS 200 Spring 20026 Dynamic Web Sites

7 10 April 2002CS 200 Spring 20027 Dynamic Web Site Client (Browser) GET /cs200/notes/presidents.php3 … Server

8 10 April 2002CS 200 Spring 20028 Dynamic Web Site Client File Server GET /cs200/notes/presidents.php3 Read ~evans/public_html/cs200/notes/presidents.php3 Presidents of the United States Presidents of the United States <? $hostName = "dbm1.itc.virginia.edu"; $userName = "dee2b"; $password = "quist"; $dbName = "dee2b_presidents"; … Request Processor

9 10 April 2002CS 200 Spring 20029 Processing a GET Request Presidents of the United States Presidents of the United States <? $hostName = "dbm1.itc.virginia.edu"; $userName = "dee2b"; $password = "quist"; $dbName = "dee2b_presidents"; … Regular HTML: Send through to client PHP Code: (inside ) Evaluate using PHP evaluator, send result to client PHP Evaluator to Client

10 10 April 2002CS 200 Spring 200210 PHP Code A universal programming language –Everything you can do in Scheme you can do in PHP –Everything you can do in PHP, you can do in Scheme Imperative Language –Designed to support a style of programming where most of the work is done using assignment

11 10 April 2002CS 200 Spring 200211 Learning New Languages Syntax –Where the {, ;, $, etc. all go –If you can read a BNF grammar, this should be easy Semantics –What does it mean –Learning the evaluation rules –Harder, but most programming languages have very similar evaluation rules

12 10 April 2002CS 200 Spring 200212 PHP If Instruction ::= if (Expression) { Instructions } Evaluate Expression. If it evaluates to true, evaluate the Instructions. It is similar to (if Expression (begin Instructions)) Difference is what “true” means. In Scheme, it means anything that is not #f. In PHP it means anything that is not 0, the empty string, or a few other things.

13 10 April 2002CS 200 Spring 200213 PHP Example $i = 1; $a = 1; $b = 1; while ($i <= 10) { print "Fibonacci $i = $b "; $next = $a + $b; $a = $b; $b = $next; $i = $i + 1; } ; Assignment: (define i 1) or (set! i 1) $i is a variable (all variables start with $ ) Instruction ::= while (Expression) { Instructions } As long as Expression evaluates to true, keep doing Instructions.

14 10 April 2002CS 200 Spring 200214 Using a Database Web requests are stateless –No history of information from previous requests To do something useful, we probably need some state that changes as people visit the site That’s what databases are for – store, manipulate, and retrieve data

15 10 April 2002CS 200 Spring 200215 Presidents of the United States Presidents of the United States <? $hostName = "dbm1.itc.virginia.edu"; $userName = "dee2b"; $password = "quist"; $dbName = "dee2b_presidents"; … Regular HTML: Send through to client PHP Code: (inside ) Evaluate using PHP evaluator, send result to client PHP Evaluator to Client Database SQL Command Values

16 10 April 2002CS 200 Spring 200216 SQL Structured Query Language (SQL) –(Almost) all databases use it Database is tables of fields containing values numberlastnamefirstnamecollege 1WashingtonGeorge 2AdamsJohnHarvard 3JeffersonThomas William and Mary

17 10 April 2002CS 200 Spring 200217 SQL Commands Create a table create table presidents ( number int primary key, lastname varchar(100), firstname varchar(100), college varchar(100), startdate date, enddate date ) ; numberlastnamefirstnamecollegestartdateenddate presidents primary key – used to uniquely select and entry in the table all fields must have a type int – integer varchar(n) – string of up to n characters

18 10 April 2002CS 200 Spring 200218 SQL: Add Entry insert into presidents (number, lastname, firstname, college, startdate, enddate) values (3, 'Jefferson', 'Thomas', 'William and Mary', '1801-03-04', '1809-03-03'); numberlastnamefirstnamecollegestartdateenddate 3JeffersonThomas William and Mary 1801-03-04 1809-03-03 presidents

19 10 April 2002CS 200 Spring 200219 SQL: Select SelectQuery ::= SELECT fields FROM table joinClause [WHERE conditions] [ORDER BY field [DESC]] SELECT * FROM presidents; SELECT lastname FROM presidents; SELECT lastname, firstname FROM presidents ORDER BY lastname; SELECT lastname, firstname FROM presidents WHERE college=‘William and Mary’ ORDER BY lastname;

20 10 April 2002CS 200 Spring 200220 Nesting Selects SELECT evaluates to a table, so of course, we can SELECT from that table SELECT lastname, firstname FROM (SELECT * FROM presidents WHERE college=‘William and Mary’) ORDER BY lastname;

21 10 April 2002CS 200 Spring 200221 SQL in PHP in HTML Presidents of the United States Presidents of the United States <? $hostName = "dbm1.itc.virginia.edu"; $userName = "dee2b"; $password = "quist"; $dbName = "dee2b_presidents"; mysql_connect($hostName, $userName, $password) or exit("Unable to connect to host $hostName"); mysql_select_db($dbName) or exit("Database $dbName does not exist on $hostName"); $result = mysql_query("SELECT lastname, firstname FROM presidents WHERE college='William and Mary' ORDER BY lastname");

22 10 April 2002CS 200 Spring 200222 $numrows = mysql_num_rows($result); $numcols = mysql_num_fields($result); print " "; print " "; for ($k = 0; $k < $numcols; $k = $k + 1) { print " ". mysql_field_name ($result, $k). " "; } print " "; for ($i = 0; $i < $numrows; $i++) { // $i++ is short for $i = $i + 1 for ($j = 0; $j < $numcols; $j++) { print " ". mysql_result ($result, $i, $j). " "; } print " "; } print " "; mysql_close(); // Close the database connection ?> evans@cs.virginia.edu

23 10 April 2002CS 200 Spring 200223 The Resulting Page

24 10 April 2002CS 200 Spring 200224 This is Powerful Just change the query to get a new page Query can be a string generated by your program! –Based on user input and hyperlink clicks See the example site $result = mysql_query("SELECT lastname, firstname FROM presidents WHERE college='William and Mary' ORDER BY lastname");

25 10 April 2002CS 200 Spring 200225 Charge For Friday: come up with at least one idea for a dynamic web site You will describe your idea at the beginning of class Friday, then work in groups to settle on project ideas


Download ppt "David Evans CS200: Computer Science University of Virginia Computer Science Class 32: Vocational Skills (How to Build."

Similar presentations


Ads by Google