270 likes | 481 Views
Digital Media Technology. Week 13. PHP. Dynamic and interactive webpages Embedded in HTML Server-side programming language. Variables $name = “Oscar Wilde” ; $number = 10 ;
E N D
Digital Media Technology Week 13
PHP • Dynamic and interactive webpages • Embedded in HTML • Server-side programming language
Variables $name = “Oscar Wilde” ; $number = 10 ; • Operators $sentence = $name . “ has written ”. $number . “ plays.” ; $half = $number / 2 ; $double = $number * 2 ;
Interactive Website HTML form PHPscript User information
A Form in HTML <html> <head> <title>Databases and Webprogramming</title> </head> <body bgcolor="#99CCFF" text="#000000"> <form action="action.php" method="get"> Enter e-mail address : <br><input type="text" name="email" size="25"> <BR> <input type="submit" value=“CHECK!"> </p> </form> </body> </html>
method=“get” : http://132.229.155.189/2007-2008/ action.php?email=info@leidenuniv.nl method=“post” : http://132.229.155.189/2007-2008/action.php
HTML Form Elements <input type="text"</input> <input type="submit" value=" GO ">
A Form in HTML <html> <head> <title>Databases and Webprogramming</title> </head> <body bgcolor="#99CCFF" text="#000000"> <form action="action.php" method="get"> Enter e-mail address : <br><input type="text" name="email" size="25"> <BR> <input type="submit" value=“CHECK!"> </p> </form> </body> </html>
Form data processed by PHP script • A so-called superglobal array will be created, depending on the method specified:$_GET or $_POST ; • Names specified can be used to refer to the values, e.g.:$_GET[“email”] ; $_POST[“email”] ;
action.php : <html><head><title>DBWP</title></head> <body bgcolor="#99CCFF" text="#000000"> <?php $email = $_GET[“email”] ; print "<p>You have typed in this e-mail address: $email.</p>" ; ?> </body> </html>
Validation of user input: example read input contains ‘@’ ? N Y write ‘email is correct’ write ‘email is not valid’
Conditional Structures: Selection if ( condition ) { [ code block 1 ] } else { [ code block 2 ] }
Conditional Structures: Selection $email = “info@leidenuniv.nl” ; if ( eregi ( “@”, $email) ) { print “Email is correct.” ; } else { print “Email is not valid. ; }
Multiple choices read language English? N Y German? Write ‘Welcome!’ Y N French? Write ‘Wil-kommen!’
Conditional Structures: Selection if ( condition ) { [ code block 1 ] } elseif ( condition ) { [ code block 2 ] } else { [ code block 3 ] }
HTML Form Elements <select> <option>Select...</option><option value="">option 1</option><option value="">option 2</option></select>
Pull-down menus <form action=“welcome.php" method="get"> <select name="language"> <option> -- Choose a language -- </option> <option value="dut">Nederlands</option> <option value="eng">English</option> <option value="fre">Français</option> <option value="ger">Deutsch</option> <option value="ita">Italiano</option> </select> <BR> <input type="submit" value="GO!"> </p> </form> this file in a browser
$language = $_GET["language"] ; if ( $language == "dut“ ) { print "Welkom op deze website!"; } elseif ( $language == “eng" ) { print "Welcome to this website!"; }elseif ( $language == “fre" ) { print "Bienvenue sur le site!"; } elseif ( $language == “ger" ) { print "Willkommen auf der Website!"; } elseif ( $language == “ita" ) { print "Benvenuto al questo site!"; } else { print "Please choose a language first!"; }
$titles = array ("Waiting for Godot", "Endgame", "Happy Days", “Krapp’s Last Tape" ); $title = "Waiting for Godot";
Arrays • Used to define a collection / a list • Can be declared with the array()function <?php $titles = array ("The Life of Dr. Boerhaave", "First Principles of Typography", "Florentissima Brittanniae Urbs ", "Digital Access to Book Trade Archives" ) ; ?>
Use of the array() function in this way will create an automatic index
Addressing elements in the array <? $books = array ( "The Life of Dr. Boerhaave", "First Principles of Typography", "Florentissima Brittanniae Urbs", "A relation of a Voyage to the Army") ; print $books[1] ?>
An indexed array : $capitals = array( "Italy"=> "Rome", "Luxembourg"=> "Luxembourg", "Belgium"=> "Brussels", "Denmark"=> “Copenhagen", "Finland"=> "Helsinki", "France"=> "Paris", "Slovakia"=> "Bratislava", "Slovenia"=> "Ljubljana", "Germany"=> "Berlin” ) ;
Addressing an element in an indexed array : $capitals = array( "Italy"=> "Rome", "Luxembourg"=> "Luxembourg", "Belgium"=> "Brussels” ) ; print $capitals["Italy"] ; // this will print "Rome"
Counting the number of elements in an array <? $books = array ("The Life of Dr. Boerhaave", "First Principles of Typography", "Florentissima Brittanniae Urbs", "Digital Acces to Booktrade Archives") ; $number = count($books) ; ?>
Navigating arrays $books = array("Heart of Darkness", "The Picture of Dorian Grey", "The Last September" ); foreach ($books as $b) { print " <p> $b </p> “ ; }
Navigating indexed arrays <? $apl_books = array ( "APL-1" => "The Life of Dr. Boerhaave", "APL-2" => "First Principles of Typography" ) ; foreach ($apl_books as $number => $title) { print “<p>Number $number: <b> $title </b> </p.” ;}