Server-side: PHP and MySQ webserver e.g. Apache ) ) PHP MySQ!" #$$%& ' )& +", -../ -0 &0/ 1 +"1& ' <head><title>hello</title></head> echo "<h1>hello</h1>" ) ; </body> </html> 1 2 '2 & 3& <7=0 45 515 )&1 54 6 7 1 81 ' +:"& & & = 1, 7 > ; http://www.apachefriends.org 3 4 7 A6<7 0 + 7=?<@A! B 7= C+" +=<D E = A F 1 PHP: Programming anguage of the Year 2004 5 6 RU Nijmegen, voorjaar 200 1
1 +"1 1 ) 1 <head><title>generation of HTM</title> </head> echo "<h3>generated HTM-code</h3>\n" ; $number = 5 ; $square = $number $number ; echo "<p>if number = $number, ", then its square = $square </p>" ; for $num = 1 ; $num < 10 ; $num++ ) echo "$num " ; <p>the end... </p> ) 1 <head> <title>generation of HTM</title> </head> <h3>generated HTM-code</h3> <p>if number = 5, then its square = 25</p> 1 2 3 4 5 6 7 8 <p>the end... </p> </body> </html> 1 7 B 'G5&H 6 ' C+"= 1 '1 I 5& 2 include 'prepend.php' ; include $somefile ; include 'somefile.txt' ) ; 8 J1 <!DOCTYPE html PUBIC "-//W3C//DTD XHTM 1.0 Strict//EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <head> <title>include files</title> <style><!- - here go the relevant styles..//--> </style></head> <div class="top"> include 'top.inc'; </div> <div class="left"> $somefile = "menu.html" ; include $somefile; </div> <div class="main"> include 'somefile.txt' ); </div> = = > = 5 5&K & 10 5 echo <h1>hello</h1> ) ; <=secure / always) @ ) ' <? echo <h1>hello</h1> ) ; <script language="php"> echo <h1>hello</h1> ) ; </script> <% echo <h1>hello</h1> ) ; %> reliant upon server configuration A < ) echo "<h1>php echo-test</h1>" ) ; $firstname = "John" ; $lastname = "Johnson" ; $age = 21 ; $length = 1.87 ; Variable names always start with the dollar sign $, e.g.: $a_variable ; $name = "John" ; $weight1 = 64.5 ; $weight2 = $weight1 1.25 ; $age = 25 ; 4M 6 '? ' & echo $firstname, " ", $lastname ; echo " is " ) ; echo $age ; echo " years old<br>and has length: $length m's."; When writing a web script, use PHP to output HTM tags, too. 11 12 RU Nijmegen, voorjaar 200 2
< ).81 N IO8811 // Create a variable $myvariable; // Do things to variables $augend = 3; $addend = 5; $sum = $addend + $augend; $also_sum = $augend; $also_sum += $addend; $zero = 0; $one = ++$zero; // Variables can be anything! $integer = 52; $floatvalue = 15.62364; $stringvalue = 'Hello World'; $string2value = "Hello again"; $boolvalue = true; D D '= P ' & function putheader $title ) { echo <<< HEADER <head> <title> $title </title> </head> HEADER ; ; function makeh1 $chaptitle ) { echo "<h1> $chaptitle </h1>"; ; function multiply $a, $b ) { return $a $b; ; // Function calls: putheader "PHP functions" ); makeh1 "Chapter 1" ); echo "12723 = ", multiply 127, 23 ); 13 <p>that's all...</p> 14 A ) '''&'& A -/..>./.-. Q...>.. & A & // Example control statements if $meal == 'Steak' ) echo 'Yum!'; echo 'Blugh!'; switch $beverage ) { case 'wine': $BAC = 0.03; break; case 'jungle juice': $BAC = 0.23; echo 'Death!'; break; for $i = 1 ; $i < 5 ; $i++ ) echo $i ; // Special comparison example if 1 === true ) echo 'Big problems.'; 15 A? 1: = $$O < 1: <head><title>php and strings </title></head> // A Simple String echo 'I am a simple string!'; // Concatenation echo 'A'. ' meets '. 'B'; // Concatenation and assignment $string1 = 'dog'; $string2 = "fleas"; $uglydog = $string1. $string2; echo "<br />", $string1, " ", $uglydog; // Outputting with tags and // Interpolating variables echo '<p> My dog is cute. </p>'; echo 'I said $string1'; echo "I said $string1"; echo "I have two {$string1s"; 16 ) Q ) ' R E1 & Numeric Associative Index Value Key Value 0 12345 First Name Hootie 1 Fish swim good. ast Name Blowfish 2 [SimpleXM Object] Species Salmon............ 17 J <head><title>array Demo</title></head> <h1>array Demo</h1> <p> $capitals = array 'France'=>'Paris', 'Ireland'=>'Dublin', 'Netherlands'=>'Amsterdam', 'Belgium'=>'Brussels'); echo 'The capital of France is '; echo $capitals['france'], ".<br />"; echo "The various capitals are:<ul>"; foreach $capitals as $city) { echo "<li>$city</li>"; ; echo "</ul>" </p> 18 RU Nijmegen, voorjaar 200 3
) & A & & 5! ) <head><title>php Arrays</title> </head> // Using Arrays $meals = array 'Steak', 'Shark', 'Cat' ) ; $steak_meal = $meals[0]; $judges = array 'Steak' => 'Good', 'Shark' => 'Dry', 'Cat' => 'Dry & Bony' ) ; // The foreach loop foreach $meals as $food ) echo "I want $food. <br />"; echo "<br />" ; foreach $judges as $food => $rev ) echo "$food is $rev <br />"; 1 )& & <head><title>more PHP arrays</title></head> / foreach example 1: value only / $arr = array 1, 2, 3, 17); echo "Content of array \$arr: " ; foreach $arr as $value ) echo "$value "; echo "<br />"; / foreach example 2: value with index printed for illustration) / $arr = array 1, 2, 3, 17); $index = 0; / for illustrative purposes only / foreach $arr as $value ) { echo "\$arr[$index] => $value.<br />"; $index++; / foreach example 3: key and value / $arr = array "one" => 1, "two" => 2, "three" => 3, "seventeen" => 17 ); foreach $arr as $key => $value ) echo "\$arr[$key] => $value.<br />"; Attention for the use of the escape symbol \ There are two syntaxes: foreach array_expression as $value ) statement ; 20 foreach array_expression as $key => $value ) statement ; 1, D1 ST?K STBJ1 1, 1 5 G+:"1H 5 <head><title>http Request</title></head> <h3>request Form</h3> <form method="post" action="http://www.cs.ru.nl/.../request.php"> Full name:<br /> <input type="text" name="fullname" size="15"> <br />City: </b><br /> <input type="text" name="city" size="15"> <br />Your order:<br /> <textarea name="ordered" cols="15" rows="3"></textarea> <p><input type="submit" value="submit"> <input type="reset" value="reset"></p> </form> 21 22 1, D1, & <head><title>request Answer</title></head> <h1>confirmation</h1> We received your request: <table border="1"><tr> <td>full name:</td> $fullname = $_POST['fullname']; echo "<td>$fullname </td>"; </tr><tr> <td>city:</td> $city = $_POST['city']; echo "<td> $city </td>"; </tr></table> <p>you ordered:<br /> $ordered = $_POST['ordered']; echo $ordered; </p> ST?1? 1 23 1 @ 5' A +:"& A +:" A I D :"1, 1 < +:" mysql_connect...,...,...) mysql_select_db...) mysql_query $query ) e.g. rows/fields/array... mysql_close ) 24 RU Nijmegen, voorjaar 200 4
Contact with which database system? +:" 1 :"+:" 1!<@+& :" = 4 M' 4 M // Connect and Select the database mysql_connect localhost, admin, mypassword ); mysql_select_db awesome_db ); // Now let s grab one single name $name = Joel ; $result = mysql_query SEECT age FROM people WHERE name= $name ; ); $age = mysql_fetch_assoc$result); MySQ is free no charge, source available) It has good PHP support Using a different SQ db system should be easy 25 People: Name Age Height Tack 1 64 Joel 1 34 echo $age[ age ]; mysql_close); Possible if only 26 one single value! And more examples...... // perform the sql-query and return the results: $result = mysql_query "SEECT FROM sample;" ); //show all the data via a while loop: while $row = mysql_fetch_assoc $result ) ) { // using foreach, list all the data that was returned in the $row array // from mysql_fetch_assoc foreach $row as $key => $value ) { echo $key. ' =. $value. '<br />' ; echo '<br />'; 27 Some information from php.net : mysql_fetch_array PHP 4, PHP 5, PEC mysql:1.0) mysql_fetch_array Fetch a result row as an associative array, a numeric array, or both Description : array mysql_fetch_array resource $result [, int $result_type] ) Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc PHP 4 >= 4.0.3, PHP 5, PEC mysql:1.0) mysql_fetch_assoc Fetch a result row as an associative array Description : array mysql_fetch_assoc resource $result ) Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc) is equivalent to calling mysql_fetch_array) with MYSQ_ASSOC for the optional second parameter. It only returns an associative array. mysql_fetch_row PHP 4, PHP 5, PEC mysql:1.0) mysql_fetch_row Fetch a result row as an enumerated array Description : array mysql_fetch_row resource $result ) Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. 28 => Which you use is up to you. All functions are pretty similar. Another mysql_fetch -possibility when using OO-php mysql_fetch_object PHP 4, PHP 5, PEC mysql:1.0) mysql_fetch_object Fetch a result row as an object Description: object mysql_fetch_object resource $result [, string $class_name [, array $params ]] ) Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead. Parameters result The result resource that is being evaluated. This result comes from a call to mysql_query). class_name The name of the class to instantiate, set the properties of and return. If not specified, a stdclass object is returned. params An optional array of parameters to pass to the constructor for class_name objects. 2 < I+:" $connected = mysql_connect...science.ru.nl", "B3test-admin",... ) ; if!$connected ) echo "System could not connect to MySQ...<br />"; echo "System connected correctly to MySQ...<br />"; $selectdb = mysql_select_db "B3test ) ; if!$selectdb ) echo "Error: could not select the database...<br />"; echo "Database was correctly opened...<br />"; $result = mysql_query "SEECT FROM table_name ; ) ; if!$result ) echo "The query could not be executed...<br />"; echo "The query executed correctly...<br />";!" #$! mysql_close ) ; 30 RU Nijmegen, voorjaar 200 5
A= 1 45 -/&M 4&M1 =!<@+' ', $host ="mysql-b3test.science.ru.nl"; $user ="B3test-user"; $password = "63GHrqw2"; $database = "B3test"; $connection = mysql_connect $host, $user, $password ) or die "Couldn't connect to server". mysql_error) ); < I+:" & //... See first part.. $result = mysql_query "SEECT Voornaam, Achternaam, Plaats FROM eden ;"); if!$result ) echo "The query could not be executed...<br />"; echo "The query executed correctly...<br />"; // dealing with the results: determine number of received rows and of fields... $num_rows = mysql_num_rows $result ); $num_fields = mysql_num_fields $result ); echo "Received: num_rows= ", $num_rows, "<br />"; echo "Received: num_fields= ", $num_fields, "<br />"; $db = mysql_select_db $database, $connection ) or die "Couldn't select database". mysql_error) ); $query = ". " ; $result = mysql_query $query ) or die "Couldn't execute query: ". mysql_error)); 31 // transfer first received row data from total $result to a separate 1-row-variable // through: $row = mysql_fetch_array $result ); // or: $row = mysql_fetch_assoc $result ); // get the column headers out of the partly associative) row-array: // through: $keys = array_keys $row ) ; // or: foreach $row as $key => $value ). 32 < I+:" 3& if $num_rows > 0 ) { $firstrow = true; echo "<table border=\"2\">"; while $row = mysql_fetch_assoc $result ) ) { if $firstrow) { echo "<tr>"; foreach $row as $key => $value) echo "<th>$key</th>"; echo "</tr>\n"; $firstrow = false; echo "<tr>"; foreach $row as $key => $value ) echo "<td> $value </td>" ; echo "</tr>" ; echo "</table>" ; First: place the table headers the $key-values) 33 < I+:" 2& $row = mysql_fetch_array $result ); $keys = array_keys $row ) ; echo "<table border=2><br /> <tr>" ; for $index=0 ; $index < $num_fields ; $index++ ) echo "<th>", $keys[ 2 $index + 1 ], "</th>" ; echo "</tr>" ; for $row_num=0; $row_num < $num_rows; $row_num++) { echo "<tr>" ; $values = array_values $row ) ; for $index=0 ; $index < $num_fields ; $index++ ) { $value = htmlspecialchars $values[2$index +1]); echo "<td> $value </td>" ; echo "</tr>" ; // fetch next result row: $row = mysql_fetch_array $result ); echo "</table>" ; A difficult way don t walk this way if not nevessary! Much simpler with the while $row = mysql_fetch_array $result ); ) and foreach $row as $key => $value ) construction with key and value from the last sheet! 34 " 7 '@6 ) A@ 777! 7 ) U1 1, I+:"1, B = < mysql-b3test.science.ru.nl B3test-user 63GHrqw2 B3test 6@2 1 F " @ I II I II,I II FI 35 V 2#& F = Groepsindeling met groupnumber : 1 Koldijk, Saskia + Sappelli, Maya 2 Veltmaat, Maaike + Wijngaarden, Carolien van 3 Dijkstra, ouis + Gerke, Paul K. 4 Neutelings, Jascha + Versteeg, Reinout 5 Haga, Femke + Verdonschot, Taco 6 Barth, Ruud + Wolterink, Jelmer 7 Dijk, Els van + Vroon, Jered 8 Fang, Yuen + Hofman, Jorien Janssen, oth + Petrachi, Daniel 10 Roerdinkholder, Hans <= dus bijv. group5 36 RU Nijmegen, voorjaar 200 6
) U1 1, I+:"1 & =F F 1 # I#R III@2 I 1I 1 = P= = @2# I @2#R8 =@1 6 1 &=solost.science.ru.nl 5&/vol/www/B3test/web-docs/group n = 1 =& 1& http://www.b3test.science.ru.nl/group'n' 1 ) ' 1 ' W 1 = 37 ) U1 1, I+:"1 += C+"1' +:"1 V ' XX = F> Zorg ervoor, dat alle gegevens per lid!) in een nette tabel getoond worden. Gebruik webpagina s die naar eigen opmaak -voorkeur via PHP met include -opdrachten en styles; géén frames meer) gegenereerd worden test dingen eerst kaal uit). 38 RU Nijmegen, voorjaar 200 7