Http://cse103 U:/msu/course/cse/103 Day 21, Slide 1 CSE 103 Makeups –If you didn’t take one over...
-
Upload
melvin-martin -
Category
Documents
-
view
214 -
download
1
Transcript of Http://cse103 U:/msu/course/cse/103 Day 21, Slide 1 CSE 103 Makeups –If you didn’t take one over...
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 11
CSE 103CSE 103
MakeupsMakeups– If you didn’t take one over the weekend, take If you didn’t take one over the weekend, take
one TUESDAY or WEDNESDAY!one TUESDAY or WEDNESDAY!– Next chance: take one between FRIDAY and Next chance: take one between FRIDAY and
TUESDAYTUESDAYBut don’t take one during the game(s); that would But don’t take one during the game(s); that would be lousybe lousy
– In-class BT: next THURSDAY (or MONDAY, In-class BT: next THURSDAY (or MONDAY, April 11) April 11)
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 22
Dynamically Generated Web Dynamically Generated Web PagesPages
Dynamic web pages are generated as Dynamic web pages are generated as needed or as requestedneeded or as requested
Allows you to make a Allows you to make a hugehuge number of number of pages from a single file using a “scripting pages from a single file using a “scripting language” such as PHPlanguage” such as PHP
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 33
Client
requests http://cse103.egr.msu.edu/user/sparty/actor.php
The web server requests data from the database server based on the PHP script
Database Server
Web Server
Serving PHP files that use a Serving PHP files that use a databasedatabase
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 44
PHP and databasesPHP and databases
PHP connects to MySQLPHP connects to MySQL– Other database servers also supportedOther database servers also supported
Specify which database to connect toSpecify which database to connect to– Today we'll use the movies databaseToday we'll use the movies database
Store queries in the PHP fileStore queries in the PHP file– Web server sends them to database serverWeb server sends them to database server– Database server sends results backDatabase server sends results back
Today we will start making a small “cast Today we will start making a small “cast browser” set of pagesbrowser” set of pages
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 55
CSE 103 functionsCSE 103 functionsTo simplify using PHP, we provide some common To simplify using PHP, we provide some common utilities for youutilities for you– Can use Can use require()require() just like just like include()include() in order to use a library in order to use a library– Functions in that file can be called from the Functions in that file can be called from the requiringrequiring file file– They save you time; not having to learn picky details about PHPThey save you time; not having to learn picky details about PHP– Feel free to examine the source code (Day 21 AFS)Feel free to examine the source code (Day 21 AFS)
Start a new PHP page named Start a new PHP page named actor.phpactor.php in in Dreamweaver. (Save in your web folder!)Dreamweaver. (Save in your web folder!)Include the function definitions from the file above Include the function definitions from the file above by entering at the top of your new page (line 1)by entering at the top of your new page (line 1)<?php<?phprequirerequire('/afs/msu/course/cse/103/php/cse103.php');('/afs/msu/course/cse/103/php/cse103.php');?>?>
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 66
Test database connectionTest database connectionUse Use set_database()set_database() to choose the database with to choose the database with– Server: "cse103.egr.msu.edu"Server: "cse103.egr.msu.edu"– Database: "movies"Database: "movies"– Username: "yourmsuNetID"Username: "yourmsuNetID"– Password: "yourPID", with starting A capitalizedPassword: "yourPID", with starting A capitalized
Add the PHP function Add the PHP function get_SQLversion()get_SQLversion() to the to the page in a page in a printprint statement in the <body> statement in the <body>– This function finds the version of the MySQL database This function finds the version of the MySQL database
server being used (server being used (4.1.8a-Debian_1-log4.1.8a-Debian_1-log))
Test the page by browsing to it:Test the page by browsing to it:http://cse103.egr.msu.edu/user/SPARTY/actor.phphttp://cse103.egr.msu.edu/user/SPARTY/actor.php
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 77
Variables in PHPVariables in PHP
Variables and constants let you store Variables and constants let you store values for later usevalues for later use– The “value” of variables can changeThe “value” of variables can change
All PHP variables begin with a All PHP variables begin with a $$ followed followed by a non-numeric characterby a non-numeric character– ExampleExample:: $name = 'Bob';$name = 'Bob'; – Assigns the value Assigns the value BobBob to the variable to the variable $name$name
Only alphabetic, numeric, and Only alphabetic, numeric, and _ _ characters are allowed in variable namescharacters are allowed in variable names
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 88
Query stringsQuery stringsAn SQL query is usually several lines longAn SQL query is usually several lines longIt is usually entered as a single string–in It is usually entered as a single string–in ""quotesquotes""It is usually assigned to a variable, so that it can It is usually assigned to a variable, so that it can easily be passed to functionseasily be passed to functionsThe query itself should The query itself should NOTNOT end with end with ; ; but the but the PHP statement PHP statement MUSTMUST end with one ( end with one (OUTSIDEOUTSIDE the quotes around the query)the quotes around the query)Example:Example:$query = "$query = "SELECT MovieTitle, YearSELECT MovieTitle, YearFROM tbl_MoviesFROM tbl_MoviesWHERE Year = 2004WHERE Year = 2004";";
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 99
Using a query to generate a Using a query to generate a dynamic dynamic pagepage
Create an SQL query that finds FirstName and Create an SQL query that finds FirstName and LastName of a person identified by LastName of a person identified by ActorIDActorID– Use Use ActorID=201101ActorID=201101 to test the query to test the query
Store this (and every query you ever use) in a Store this (and every query you ever use) in a variablevariable
Use Use print print and theand the text_from_query() text_from_query() function to function to display the actor’s namedisplay the actor’s name– Check the Day 21 classwork page for Check the Day 21 classwork page for
documentation linkdocumentation link
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 1010
Using a query to generate a Using a query to generate a dynamic dynamic tabletable
Create a second SQL query that finds each Create a second SQL query that finds each movie, year, and role in which s/he has movie, year, and role in which s/he has appeared – ordered by yearappeared – ordered by year– Continue to restrict by ID numberContinue to restrict by ID number
Make a table on your page of the results of this Make a table on your page of the results of this query using query using printprint and and table_from_query()table_from_query()– Check the support function documentation Check the support function documentation
linked from the Day 21 page if you need tolinked from the Day 21 page if you need to– Very similar to using text_from_query()Very similar to using text_from_query()
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 1111
Movie informationMovie informationCreate a new PHP page called Create a new PHP page called movie.phpmovie.php
Display name & year of movie (given Display name & year of movie (given MovieIDMovieID) ) as a heading on the page (in as a heading on the page (in <h1><h1> tags) tags)– Test with Test with MovieID = MovieID = 130990130990
Make a table displaying each credit number*, Make a table displaying each credit number*, role in the movie, and name of actor playing itrole in the movie, and name of actor playing it– Order by Order by CreditNumber CreditNumber (*this field is in tbl_Roles)(*this field is in tbl_Roles)– Use the MySQL Use the MySQL concat()concat() function to put both first function to put both first
and last name of the actor into a single fieldand last name of the actor into a single field
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 1212
Passing information to a Passing information to a dynamic pagedynamic page
ParametersParameters can be passed in a URL can be passed in a URL
Example:Example:– ……/actor.php/actor.php?ActorID=201101?ActorID=201101
Same syntax can be used by forms in a Same syntax can be used by forms in a GETGET operation operation – We will look at this next timeWe will look at this next time
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 1313
Using parameters in PHPUsing parameters in PHPEach parameter becomes a special PHP variable, Each parameter becomes a special PHP variable, of the form of the form $_GET$_GET['['keykey']=']=valuevalue, where , where keykey is the is the name before = and value is after:name before = and value is after:– ……/actor.php?/actor.php?ActorIDActorID==12341234
makes $_GET['makes $_GET['ActorIDActorID']=']=12341234– KeyKey is case-sensitive: $_GET['actorid']=0 is case-sensitive: $_GET['actorid']=0
This value can be used later in PHPThis value can be used later in PHPIf you need to use it inside a quoted string, like a If you need to use it inside a quoted string, like a query, put curly braces { } around it:query, put curly braces { } around it:$query = $query = "…WHERE ActorID= "…WHERE ActorID={$_GET['ActorID']} {$_GET['ActorID']} …";…";
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 1414
Exercise - Using parameters in Exercise - Using parameters in PHPPHP
Modify the Modify the actor.phpactor.php page to page to GETGET the value the value of ActorID from a parameterof ActorID from a parameter
Change your queries to use the correct Change your queries to use the correct $_GET['xyz'] variable$_GET['xyz'] variable
Test cases:Test cases:– ActorID=615264ActorID=615264 is Sandra Bullock and is Sandra Bullock and
ActorID=165619ActorID=165619 is Keanu Reeves is Keanu Reeves
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 1515
Exercise – Using parameters againExercise – Using parameters again
Now modify the Now modify the movie.phpmovie.php page to page to GETGET the value of MovieID from a parameterthe value of MovieID from a parameter
Change your queries to use the correct Change your queries to use the correct $_GET['xyz'] variable$_GET['xyz'] variable
Test cases:Test cases:– MovieID=309772MovieID=309772 is is Star Wars IIIStar Wars III and and
MovieID=130634MovieID=130634 is is RobotsRobots
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 21, Slide Day 21, Slide 1616
HomeworkHomework
Check the link on today’s classwork Check the link on today’s classwork pagepage – Finish classworkFinish classwork
– Read rest of Day 20 textbook, covering Read rest of Day 20 textbook, covering HTML formsHTML forms
– Work on UBTs!Work on UBTs!