Download presentation
Presentation is loading. Please wait.
Published byJob Wheeler Modified over 9 years ago
1
David Evans http://www.cs.virginia.edu/evans CS200: Computer Science University of Virginia Computer Science Class 29: Vocational Skills How to Build a Dynamic Web Site
2
31 March 2004CS 200 Spring 20042 Vocational Skills SQL PHP HTML Scheme Job listings at monster.com (31 March 2004) : “more than 5000” $30-50K 203 in last 24 hours 785 $40-$150K “more than 5000”$80-$400K 3570 posted in last 14 days 0 $200K?
3
31 March 2004CS 200 Spring 20043 Covers topics primarily selected for their practical (not intellectual) value Covers material found in “For Dummies” books (but we’ll cover it differently) There is no “Computability Theory for Dummies”, “Complexity Theory for Dummies”, “Higher Order Procedures for Dummies”, “Recursive Definitions for Dummies”, etc. book Today’s Aberrant Class
4
31 March 2004CS 200 Spring 20044 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
5
31 March 2004CS 200 Spring 20045 HTML: HyperText Markup Language Language for controlling presentation of web pages Uses formatting tags –Enclosed between Not a powerful language (we will learn what this means more precisely next week) –no way to make procedures or jump
6
31 March 2004CS 200 Spring 20046 HTML Grammar Excerpt Document ::= Header Body Header ::= HeadElements HeadElements ::= HeadElement HeadElements HeadElements ::= HeadElement ::= Element Body ::= Elements Elements ::= Element Elements Elements ::= Element ::= Element Make Element a paragraph. Element ::= Element Center Element horizontally on the page. Element ::= Element Display Element in bold. Element ::= Text What is a HTML interpreter?
7
31 March 2004CS 200 Spring 20047 Dynamic Web Sites Programs that run on the client’s machine –Java, JavaScript, Flash, etc.: language must be supported by the client’s browser (so they are usually flaky and don’t work for most visitors) –Used (almost exclusively) to make annoying animations to make advertisements more noticeable Programs that run on the web server –Can be written in any language, just need a way to connect the web server to the program –Program generates regular HTML – works for everyone –(Almost) Every useful web site does this
8
31 March 2004CS 200 Spring 20048 Dynamic Web Site Client (Web Browser) “HTML Interpreter” GET /cs200/notes/presidents.php3 … Server
9
31 March 2004CS 200 Spring 20049 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
10
31 March 2004CS 200 Spring 200410 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
11
31 March 2004CS 200 Spring 200411 PHP Code A universal programming language –Everything you can compute in Scheme you can compute in PHP –Everything you can compute in PHP, you can compute in Scheme –Friday we will explain why more convincingly Imperative Language –Designed to support a style of programming where most of the work is done using assignment
12
31 March 2004CS 200 Spring 200412 Learning New Languages Syntax: Where the {, ;, $, etc. all go –If you can understand a BNF grammar, this is easy Semantics: What does it mean –Learning the evaluation rules –Harder, but most programming languages have very similar evaluation rules Style –What are the idioms and customs of experienced programmers in that language? Takes many years to learn Need it to be a “professional” PHP programmer, but not to make a useful program
13
31 March 2004CS 200 Spring 200413 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.
14
31 March 2004CS 200 Spring 200414 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.
15
31 March 2004CS 200 Spring 200415 Using a Database HTTP is 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
16
31 March 2004CS 200 Spring 200416 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
17
31 March 2004CS 200 Spring 200417 SQL Structured Query Language (SQL) –(Almost) all databases use it Database is tables of fields containing values All fields have a type (and may have other attributes like UNIQUE)
18
31 March 2004CS 200 Spring 200418 Demo http://www.cs.virginia.edu/csdb/
19
31 March 2004CS 200 Spring 200419 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
20
31 March 2004CS 200 Spring 200420 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
21
31 March 2004CS 200 Spring 200421 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;
22
31 March 2004CS 200 Spring 200422 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;
23
31 March 2004CS 200 Spring 200423 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");
24
31 March 2004CS 200 Spring 200424 $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
25
31 March 2004CS 200 Spring 200425 The Resulting Page
26
31 March 2004CS 200 Spring 200426 Charge Combining PHP, SQL and HTML is Very Powerful Query can be a string generated by your program! –Based on user input and hyperlink clicks Code can be generated based on what is in the database
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.