course slides -- powerpoint


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

course slides -- powerpoint

  1. 1. Introduction to Web Programming with PHP Scripting Language for Applications in Agricultural Climatology Title
  2. 2. Course Outline <ul><li>Background & Introduction </li></ul><ul><li>Resources Setup </li></ul><ul><li>Input </li></ul><ul><li>Comment & Output </li></ul><ul><li>Data Type </li></ul><ul><li>Variable Scope </li></ul><ul><li>Control Flow </li></ul><ul><li>Function </li></ul><ul><li>File </li></ul><ul><li>Input (combo box) </li></ul><ul><li>Project Exercises </li></ul>
  3. 3. Background <ul><li>Why web programming </li></ul><ul><li>Broader impact </li></ul><ul><ul><li>Increasing use of internet </li></ul></ul><ul><li>Exclusivity </li></ul><ul><ul><li>Restriction & monitoring ability </li></ul></ul><ul><li>Increasing interests in dynamic web, ie: </li></ul><ul><ul><li>Simple conversion (Rothamsted Research, England) </li></ul></ul><ul><ul><li>Water balance (University of Georgia) </li></ul></ul><ul><li>Why PHP? </li></ul><ul><li>Efficient delivery </li></ul><ul><ul><li>No compiler (developer) </li></ul></ul><ul><ul><li>No installation (user) </li></ul></ul><ul><li>Potential link to database (ie. Mysql) </li></ul><ul><li>Open source & flexible syntax </li></ul>Source: International Telecommunication Union (ITU)
  4. 4. About PHP <ul><li>Php </li></ul><ul><li>Recursive acronym: Hypertext Preprocessor </li></ul><ul><li>Open source scripting language suited for web application </li></ul><ul><li>Server side execution rather than client-side (ie. JavaScript) </li></ul><ul><li>Requirement: Server (web host) supports PHP </li></ul><ul><li>Characteristic: somewhat flexible syntax </li></ul>Server Client javascript browser php web-host
  5. 5. Server & File Transfer <ul><li>Example of Php-enabled server </li></ul><ul><li>HPRCC - SSH approach </li></ul><ul><li>AgilityHoster - FTP approach </li></ul><ul><li>File transfer methods </li></ul><ul><li>SSH </li></ul><ul><ul><li>SSH - Secure Shelln, a network protocol that allows data to be exchanged using a secure channel between two networked devices </li></ul></ul><ul><ul><li>SSH is used primarily on Linux and Unix based systems to access shell accounts. </li></ul></ul><ul><ul><li>SSH is a replacement for telnet and other insecure remote shells </li></ul></ul><ul><li>FTP </li></ul><ul><ul><li>FTP – file transfer protocol </li></ul></ul><ul><ul><li>FTP is built on a client-server architecture </li></ul></ul>
  6. 6. Resources <ul><li>For this course, you will also set up resources for writing php codes </li></ul><ul><li>File transfer: WSFTP </li></ul><ul><li>Text editor: Texpad </li></ul><ul><li>Download the resources </li></ul><ul><li>Setting up WSFTP </li></ul><ul><ul><li>Enter new profile name, ie: php training agilityhoster </li></ul></ul><ul><ul><li>Enter host name/address: </li></ul></ul><ul><ul><li>UserID: -- should be automatically filled </li></ul></ul><ul><ul><li>Password: enter the custom password you set up previously </li></ul></ul><ul><ul><li>The Ftp is ready to do file transfer </li></ul></ul><ul><ul><li>Create a folder in your hardrive to store all your web/php files </li></ul></ul>
  7. 7. Basic syntax & first exercise <ul><li>Basic syntax </li></ul><ul><li>start and end processing instruction </li></ul><ul><li>Semicollon (;) after each statement, case insensitive! </li></ul><ul><li>First exercise (hello world!) </li></ul><?php ?> <html> <head> <title>PHP Test</title> </head> <body> <?php echo &quot;<p>Hello World</p>&quot;; ?> </body> </html>
  8. 8. Input (basic) <ul><li>Required HTML form </li></ul><ul><li>Functions: </li></ul><ul><li>isset - Determines if a variable is set and is not NULL. </li></ul><ul><li>$_POST['variable'] is an associative array of variables passed to the current script via the HTTP POST method. </li></ul><ul><li>$_POST is a superglobal contains information pertinent to any parameters passed using the POST method. </li></ul><ul><ul><li>POST is a preferred option (the other option is GET) as it can handle considerably more data </li></ul></ul><ul><ul><li>A POST request includes a message body in addition to a URL used to specify information for the action being performed. </li></ul></ul><ul><ul><li>Example of URL encoding is as follow: </li></ul></ul><ul><li>Name: Jonathan Doe </li></ul><ul><li>Age: 23 </li></ul><ul><li>Formula: a + b == 13%! </li></ul><ul><li>Encoded as: </li></ul><ul><li>Name=Jonathan+Doe&Age=23&Formula=a+%2B+b+%3D%3D+13%25%21 </li></ul>
  9. 9. Input (syntax) Syntax (example): View this example <html> <head> <title>PHP Test</title> </head> <body> <?php if (isset($_POST['submit'])) { echo &quot;Hi, &quot; . $_POST['name']. &quot;! <br/>&quot;; } ?> <form action=&quot;formexample.php&quot; method =&quot;post&quot;> <p> Name: <br/> <input type = &quot;text&quot; name=&quot;name&quot; size =&quot;20&quot; maxlength =&quot;40&quot; value=&quot;&quot; /> </p> <input type=&quot;submit&quot; name = &quot;submit&quot; value&quot;Go!&quot; /> </form> </body> </html>
  10. 10. Comments & Output <ul><li>Comments </li></ul><ul><li>Output </li></ul><ul><li>Print </li></ul><ul><ul><li>displaying output to screen </li></ul></ul><ul><ul><li>return boolean </li></ul></ul>Single line // comment Multi line /* comment comment */ <?php print (&quot;test print&quot;); print &quot;test print&quot;; ?>
  11. 11. Output (cont'd) <ul><li>Output </li></ul><ul><li>echo </li></ul><ul><ul><li>displaying output to screen </li></ul></ul><ul><ul><li>return void </li></ul></ul><ul><ul><li>identical to print </li></ul></ul><ul><ul><ul><li>theoretically echo would be faster (efficient) as it returns nothing, however, the difference is impossible to notice. The decision to use is rather stylistic concern. </li></ul></ul></ul><ul><li>printf() </li></ul><ul><ul><li>displaying formatted output to screen </li></ul></ul><ul><ul><li>example: printf(&quot;01.2f&quot;, 43.2); // 43.20 </li></ul></ul><ul><ul><li>for description of format variables, go to: </li></ul></ul><ul><li>sprintf() </li></ul><ul><ul><li>identical to printf(), but the output is assigned to a string variable </li></ul></ul><ul><ul><li>example: </li></ul></ul><ul><li>$var = sprintf(&quot;%01.2f&quot;,43.2); </li></ul><ul><li>echo &quot;$var&quot;; //43.20 </li></ul>
  12. 12. Data types Boolean (true or false) $variable = false; // $variable is false $variable = 0; // $variable is false $variable = true; // $variable is true $variable = 1; // $variable is true $variable = 5; // $variable is true Any non-zero value – true Integer Whole number, no fractional parts Maximum supported integer size is typicalla 2^31 Float Floating-point number -- contains fractional parts. String Sequence of character treated as a contiguous group
  13. 13. Data types (cont'd) Array Series of similar items. Index collection of data values. Each index (key) references to a corresponding value. Example: member[0] = &quot;John&quot;; member[1] = &quot;Marry&quot;; member[2] = &quot;Suzzie&quot;;
  14. 14. Data types (cont'd) Object Must be explicitly declared: Class -- attribute, function Class definition serves as template and cannot itself be manipulated. Instead, objects are created based on this template -- use 'new' keyword. soybean object is created and can then it attribute can be set by making us of the method setGroup() Class plant { private $group; function setGroup($status){ $this->group = $status; } } $soybean = new plant; $soybean->setGroup(&quot;dicots&quot;);
  15. 15. Type casting Converting one type to the other example: $variable1 = 13; $variable2 = (double) $variable1; // 13.0 $variable1 = 4.2; $variable2 = (int) $variable1; // 4 (truncated, not round)
  16. 16. Variables Scope Local Variables A variable declared in a function is considered local. It can only be referenced in that function. When you exit the function in which a local variable has been declared, that variable and its corresponding value are destroyed. View this example $x = 2; function assignx() { $x = 0; echo &quot;x inside function is $x <br>&quot;; } assignx(); echo &quot;x outside function is $x <br>&quot;;
  17. 17. Variables Scope (cont'd) Global Variables Global variables can be accessed in any part of the program To modify global variabl, it must be explicitly declared to be a global in the function in which itis to be modified. View this example $x = 2; function assignx() { Global $x; $x = 0; echo &quot;x inside function is $x <br>&quot;; } assignx(); echo &quot;x outside function is $x <br>&quot;;
  18. 18. Variables Scope (cont'd) Static Variables Static variable does not lose its value when the function exists. View example without static View example with static function keep_track() { Static $count = 0; $count++; echo &quot;$count <br>&quot;; } keep_track(); keep_track(); keep_track();
  19. 19. Variables Scope (cont'd) Superglobal Variables Predefined variables View example HTTP_USER_AGENT provide user's browser information For more examples of similar superglobal variables, go to: echo &quot;Your browser is: $_SERVER['HTTP_USER_AGENT']&quot;;
  20. 20. Operators For list of operators in php, go to: The list includes: arithmetic operators (*, /, +, -) string operator, ie concatenate with &quot;.&quot; logical operators (&&, ||) equality operators (==, !=)
  21. 21. Control flow Conditional Controlling the flow of execution depending on the specified condition defined by expression Exercise 1: if statement – thermal time calculation Exercise file View output if (expression) { statement; } if (expression) { statement; } else { statement; } if (expression) { statement; } else if (expression) { statement; } else { statement; }
  22. 22. Control flow (cont'd) Conditional Use as variant of if-else combination when need to compare with large number of values switch($catagory) { case &quot;1&quot;: statement1; break; case &quot;2&quot;: statement2; break; case &quot;3&quot;: statement3; break; case &quot;4&quot;: statement4; break; }
  23. 23. Control flow (cont'd) While loop Specifies a condition that must be met before execution is terminated View example *We will be using 'while loop' in the final project exercise, to read weather data while (expression) { statements; } //Example: incremental value global $i; $i = 0; while ($i <= 5) { echo &quot;$i <br>&quot;; $i++; }
  24. 24. Control flow (cont'd) For loop Statement which allows code to be repeatedly executed View example *We will be using 'for loop' in the final project exercise, to perform calculation of cumulative thermal time for (expression1; expression2; expression3) { statements; } //Example: Incremental values global $i; for ($i=0; $i<=5; $i++) { echo &quot;$i <br>&quot;; }
  25. 25. Functions Consist of: (1) Function definition, (2) Function implementation View example function geo_distance_exercise view output function definition ------------------------ function function_name (parameters) { statements; } //function definition: converting degrees to radians function rad($deg_in) { $result = $deg_in *(pi()/180); return $result; } //function implementation: converting degrees to radians $radvalue = rad(30.0); echo &quot;radvalue= $radvalue <br>&quot;; //radvalue= 0.523598775598 function implementation --------------------------------- //value-returning function: $value = function_name(parameters); //non-returning function: function_name(parameters);
  26. 26. Arrays Series of similar items, accessible via index Index can be integer or string Array size is accessible by using 'count()' function //Basic syntax to access array's item $array_name[index] = array value Exercise 2: array Exercise file View output
  27. 27. File Server Client php codes files php codes files File Reading & Writing Steps: (1) Connecting file handler to a file (2) Capturing file content (3) Closing file (ending file handler connection to a file)
  28. 28. File Reading File Reading //assigning file handler to a file $file_handler = fopen(&quot;filename&quot;,&quot;mode&quot;); //capturing file content $var = fgetcsv($file_handler,1000,&quot;,&quot;); //closing file fclose($file_handler); 1000 - maximum char per line mode: r = read only w = write only rt = read only, text file more on modes:
  29. 29. File Reading (cont'd) File Reading Exercise Exercise file View output Create &quot;input_files&quot; directory in the ftp folder Create date.txt file with the following content: 10 20 30 30 45
  30. 30. File Writing File Writing File Writing Exercise Exercise file View output Create &quot;output_files&quot; directory in the ftp folder //assigning file handler to a file $file_handler = fopen(&quot;filename&quot;,&quot;mode&quot;); //writing file fwrite($filehandler,&quot;$variable_to_write&quot;); //closing file fclose($file_handler); mode: w = write only, wt = write only, text file, a = append more on modes:
  31. 31. File Reading – Multiple Columns Multiple columns of data are common, ie: weather data File Reading Example – Multi Columns View code View output Trick: Create an intermediate 'clean' file (two reading steps) The 'clean' file has a consistent delimited, ie: one space User ereg_replace() function to clean extra white spaces $originalvar = &quot;10 20&quot;; $cleanvar = ereg_replace(' +', ' ', $originalvar); echo &quot;$cleanvar&quot;; // 10 20 10 20 20 40 30 60 30 60 45 90 File to read: data2.txt
  32. 32. Combo box Example View this example View code Month: <select name=&quot;month&quot;> <option value=&quot;1&quot;>Jan</option> <option value=&quot;2&quot;>Feb</option> <option value=&quot;3&quot;>Mar</option> <option value=&quot;4&quot;>Apr</option> <option value=&quot;5&quot; Selected>May</option> <option value=&quot;6&quot;>Jun</option> <option value=&quot;7&quot;>Jul</option> <option value=&quot;8&quot;>Aug</option> <option value=&quot;9&quot;>Sep</option> <option value=&quot;10&quot;>Oct</option> <option value=&quot;11&quot;>Nov</option> <option value=&quot;12&quot;>Dec</option> </select>
  33. 33. Project Exercise Goal Reading weather file from long-term weather data from NASA Eventually calculate cumulative thermal time (GDU) for a user-given starting and ending dates Preparation Download new weather data from NASA site *, or use the existing weather data for Lincoln, NE * Required lat & long input Online resource for finding lat long of a location: itouchmap Save weather file as text file under your input_files folder
  34. 34. Project Exercise 1 <ul><li>Objective </li></ul><ul><ul><li>Reading weather file (NASA-DSSAT format) </li></ul></ul><ul><ul><li>Output tmax, tmin, and tmean for the first 10 days </li></ul></ul><ul><li>Topics covered </li></ul><ul><li>- Form </li></ul><ul><li>- Combo box </li></ul><ul><li>- File reading </li></ul><ul><li>- For loop </li></ul><ul><li>- While loop </li></ul><ul><li>- If statement </li></ul><ul><li>- String manipulation (ereg_replace) </li></ul><ul><li>- Functions (date->doy conversion) </li></ul>
  35. 35. Project Exercise 1 (cont'd) View output weather data: Lincoln, NE lat: 40.82, long: -96.65, 1997-2008 To skip a header line, you can assign it to a dummy variable $header = fgets($file_handler); Additional cleaning step would be needed if there is extra whie space to the left of the first column of the data. You can use the ltrim approach: Example: $array_output[$i] = ltrim($array_output[$i]); The goal is to read tmax and tmin. However, the data contains other weather variables (ie. solar radiation, rh, ect). You can assign dummy variables for the extra variables, or it doesn't hurt to assign each to uniqe variable (in case you would need it for something else) Particular important variable is tmean, not in the data, it's calculated as: Tmean = 0.5 * (Tmax + Tmin)
  36. 36. Project Exercise 2 <ul><li>Objective </li></ul><ul><ul><li>Reading weather file (NASA-DSSAT format) </li></ul></ul><ul><ul><li>Output tmax, tmin, and tmean for the first 10 days from a defined starting date </li></ul></ul><ul><li>Topics covered </li></ul><ul><li>- Form </li></ul><ul><li>- Combo box </li></ul><ul><li>- File reading </li></ul><ul><li>- For loop </li></ul><ul><li>- While loop </li></ul><ul><li>- If statement </li></ul><ul><li>- String manipulation (ereg_replace) </li></ul><ul><li>- Functions (date->doy conversion) </li></ul>
  37. 37. Project Exercise 2 (cont'd) View output Table of day of year vs date Use practically all the component from the previous exercise For day selection combo box, you can use for loop, ie from i=1 to i=31 View Example The conversion from Date to DOY is needed because the user input is date while the weather data is index by year and DOY. View Date to DOY function Use if statement to identify starting point (year & doy) to start displaying the output, within a for loop going thru the whole index of data. ie: for ($i=0; $i<=$array_size; $i++) { if (($year_array[$i] == $yearinput) && ($doy_array[$i] >= $doyinput) && ($doy_array[$i] < $doyval+9)) { //put output statement here } } Use $_POST['var'] to capture form inputs $yearval = $_POST['year']; $monthval = $_POST['month']; $dayval = $_POST['day'];
  38. 38. Project Exercise 3 <ul><li>Objective </li></ul><ul><ul><li>Reading weather file (NASA-DSSAT format) </li></ul></ul><ul><ul><li>Output tmax, tmin, and tmean, thermal time, and cumulative thermal time for a given range between starting and ending dates </li></ul></ul><ul><li>Topics covered </li></ul><ul><li>- Form </li></ul><ul><li>- Combo box </li></ul><ul><li>- File reading </li></ul><ul><li>- For loop </li></ul><ul><li>- While loop </li></ul><ul><li>- If statement </li></ul><ul><li>- String manipulation (ereg_replace) </li></ul><ul><li>- Functions (date->doy conversion) </li></ul>
  39. 39. Project Exercise 3 (cont'd) <ul><li>Everything the same as the previous exercise, except: </li></ul><ul><li>add inputs in the forms also for the ending date </li></ul><ul><li>add text inputs for 'tbase' and 'topt' </li></ul><ul><li>tbase = temperature below which plant growth/development rate equals zero </li></ul><ul><li>topt = temperature at which plant growth/development rate is maximal and thereafter it is to continue at such maximal rate </li></ul><ul><li>Modify if statement to identify both starting and ending point (year & doy) to start displaying the output & calculating thermal time & it's cumulative value </li></ul><ul><li>ie: </li></ul><ul><li>if (($year_array[$i] >= $yearval) && ($doy_array[$i] >= $doyval) && ($year_array[$i] <= $yearendval) && ($doy_array[$i] <= $doyendval)) </li></ul><ul><li>Use the previous thermal time algorithm, but should applied as a function (see example ) </li></ul><ul><li>To calculate cumulative thermal time, use the following accumulation algorithm: </li></ul><ul><li>$cvalue = $cvalue + $value_array[$i]; </li></ul><ul><li>To obtain final cumulative thermal time value, you can use max function: </li></ul><ul><li>$CTTfinal = max($cval_array)); </li></ul>View output
  40. 40. Misc. Definitions <ul><li>Uniform Resource Locators (URLs) </li></ul><ul><li>URLs provide the hypertext links between one document and another. It specifies where an identified resource is available and the mechanism for retrieving it. </li></ul><ul><li>Hypertext Markup Language (HTML) </li></ul><ul><li>WWW documents contain a mixture of directives (markup), and text or graphics. It provides a means to describe the structure of text-based information in a document—by denoting certain text as links, headings, paragraphs, lists, et. </li></ul><ul><li>The World Wide Web is composed primarily of HTML documents transmitted from Web servers to Web browsers using the Hypertext Transfer Protocol (HTTP). </li></ul>