+ All Categories
Home > Documents > PHP Application Overview and Example May 27, 2004 Presented By: Mark Armstrong and Marc Saccucci.

PHP Application Overview and Example May 27, 2004 Presented By: Mark Armstrong and Marc Saccucci.

Date post: 26-Dec-2015
Category:
Upload: oswald-fletcher
View: 214 times
Download: 0 times
Share this document with a friend
Popular Tags:
25
PHP PHP Application Overview and Example Application Overview and Example May 27, 2004 May 27, 2004 Presented By: Mark Armstrong and Presented By: Mark Armstrong and Marc Saccucci Marc Saccucci
Transcript

PHPPHP

Application Overview and ExampleApplication Overview and Example

May 27, 2004May 27, 2004

Presented By: Mark Armstrong andPresented By: Mark Armstrong and

Marc SaccucciMarc Saccucci

PHP BenefitsPHP Benefits• Not proprietary Not proprietary

• Fast - 10-50% faster then other Fast - 10-50% faster then other HTML embedded scripting (CGI)HTML embedded scripting (CGI)

• StableStable

• Familiar – syntactically similar to Familiar – syntactically similar to Java/Perl/C/C++Java/Perl/C/C++

• Compatible with nearly all platformsCompatible with nearly all platforms

• DynamicDynamic

How PHP WorksHow PHP Works

• Uses Apache Web Server, which Uses Apache Web Server, which handles HTTP requests and sends handles HTTP requests and sends HTML responses to the browser.HTML responses to the browser.

• Apache can be configured to Apache can be configured to render .php files as HTML.render .php files as HTML.

Dynamic ApplicationsDynamic Applications

• Database access (MySQL, INFORMIX, Database access (MySQL, INFORMIX, Oracle, etc)Oracle, etc)

• Text File I/OText File I/O

• XMLXML

• Parsing other websitesParsing other websites

PHP vs. CGIPHP vs. CGI

• Embedded in HTML Embedded in HTML Code Code

• All code in one All code in one locationlocation

• FastFast

• ScaleableScaleable

• Not compiledNot compiled

• Separate CGI script Separate CGI script loaded for each loaded for each request, called by request, called by HTML pageHTML page

• SlowSlow

• Not very scaleableNot very scaleable

PHP SyntaxPHP Syntax

• Start and end tags needed:Start and end tags needed:<?php<?php

PHP Code Here;PHP Code Here;

?>?>

• Syntactically very close to CSyntactically very close to C

• Semicolon after each PHP statementSemicolon after each PHP statement

• Comments start with // or #Comments start with // or #

• Variables preceded with a $Variables preceded with a $

• Can be object orientedCan be object oriented

GET and POST Form ActionsGET and POST Form Actions

• Used to pass information to a pageUsed to pass information to a page

• POST method is used when passing POST method is used when passing ‘hidden’ information via forms‘hidden’ information via forms

• GET method is used to create a query string GET method is used to create a query string which passes variables via the URLwhich passes variables via the URL

• POST can pass an unlimited amount of dataPOST can pass an unlimited amount of data

• GET can only pass up to a 1,000 character GET can only pass up to a 1,000 character query stringquery string

Query StringsQuery Strings

• Use the GET method to pass query strings via the URL.Use the GET method to pass query strings via the URL.- Example:- Example:

http:/http://www.weather.gov/forecasts/graphical/sectors/marylandWeek.php?page=1&elemen/www.weather.gov/forecasts/graphical/sectors/marylandWeek.php?page=1&element=Wxt=Wx

$_GET[“page”]=1 and $_GET[“element”]=Wx is used in the php code to $_GET[“page”]=1 and $_GET[“element”]=Wx is used in the php code to grab the page number and weather element type from the URL above.grab the page number and weather element type from the URL above.

Example Application of PHPExample Application of PHP

Main Code: display_consistency.phpMain Code: display_consistency.php<html><html><head><head> <title><title> Consistency Summary PageConsistency Summary Page </title></title> <link rel="stylesheet" type="text/css" href="../style.css"><link rel="stylesheet" type="text/css" href="../style.css"></head></head><body leftmargin="0" topmargin="0" rightmargin="0" <body leftmargin="0" topmargin="0" rightmargin="0"

bgcolor="#FFFFFF">bgcolor="#FFFFFF"> <!– Start PHP Header --><!– Start PHP Header -->

<?php include'../includes/header.php'; ?><?php include'../includes/header.php'; ?> <!-- End Header. Back to HTML. --><!-- End Header. Back to HTML. --> <table width="800" border="0" cellspacing="2"><table width="800" border="0" cellspacing="2"> <tr><tr> <td valign="top"><td valign="top"> <!-- Left Navigation/Spacer Region --><!-- Left Navigation/Spacer Region --> <!– place holder for leftnav.php -- ><!– place holder for leftnav.php -- > &nbsp;&nbsp; <!-- Left Navigation/Spacer Region --><!-- Left Navigation/Spacer Region --> </td></td> <td valign="top"><td valign="top"> <!-- Begin Content area --><!-- Begin Content area -->

Description: Description:

• First, we’ll create the basic HTML First, we’ll create the basic HTML header and open the php code header and open the php code block with the <?php to include block with the <?php to include the header for the page.the header for the page.

display_consistency.php - continueddisplay_consistency.php - continued

<?php<?php/********************************/******************************** * This page displays consistency data * This page displays consistency data for the specified day, neighboring wfo for the specified day, neighboring wfo or element specified as spec in the or element specified as spec in the querystring, and the wfo sent in the querystring, and the wfo sent in the querystring.querystring.********************************/********************************/ // Set up basic parameters and querystring values// Set up basic parameters and querystring values. . $cycle = "hourly"; $cycle = "hourly"; // variables are always preceded by a $// variables are always preceded by a $ $wfo=$_GET["wfo"]; $wfo=$_GET["wfo"]; // $_Get array contains the form values// $_Get array contains the form values $spec=$_GET["spec"]; $spec=$_GET["spec"]; // _GET method and querystrings.// _GET method and querystrings. // Set the region based on the WFO ID. // Set the region based on the WFO ID. // The substr function retrieves a substring of the string variable // The substr function retrieves a substring of the string variable // passed in. In this case, the first character from $wfo is extracted.// passed in. In this case, the first character from $wfo is extracted. if ((substr($wfo,0,1) == "K") || $wfo == "CONUS")if ((substr($wfo,0,1) == "K") || $wfo == "CONUS") $region="CONUS";$region="CONUS"; elseelse $region="ALASKA";$region="ALASKA";

Description: Description:

• Open PHP using: <?php, and Open PHP using: <?php, and start to set some of the start to set some of the variables that will be needed.variables that will be needed.

• We fetch the “wfo” and “spec” We fetch the “wfo” and “spec” parameters from the parameters from the querystring.querystring.

• Notice the C-style comments Notice the C-style comments and if constructand if construct

display_consistency.php - continueddisplay_consistency.php - continued

// Set up arrays for menu bar items…// Set up arrays for menu bar items…// using the array() function is one way to// using the array() function is one way to// initialize arrays// initialize arrays$elementList=array("MaxT","MinT","PoP12","QPF",$elementList=array("MaxT","MinT","PoP12","QPF",

"Sky","SnowAmt","T","Td","WaveHeight","Sky","SnowAmt","T","Td","WaveHeight", "WindDir","WindSpd");"WindDir","WindSpd");$day_list=array("Day0","Day1","Day2","Day3",$day_list=array("Day0","Day1","Day2","Day3", "Day4","Day5","Day6","Day7");"Day4","Day5","Day6","Day7");// Like most scripting languages, PHP can use the// Like most scripting languages, PHP can use the// echo command to output data.// echo command to output data.// PHP also uses C output commands as well.// PHP also uses C output commands as well.// The "." concatenates strings// The "." concatenates stringsecho "<br><center><h3>Consistency Summary echo "<br><center><h3>Consistency Summary

for the $cycle cycle for " . $wfo . for the $cycle cycle for " . $wfo . "</h3></center>\n";"</h3></center>\n";

echo "<center><a echo "<center><a href=\"consistency_summary_display.php?href=\"consistency_summary_display.php?wfo=" . $wfo . "\"><< Back to Summary wfo=" . $wfo . "\"><< Back to Summary </a></center><br>\n";</a></center><br>\n";

Description: Description:

• Next we set up some arrays using Next we set up some arrays using the array() function. the array() function.

• We then use php to echo some We then use php to echo some html code. There is a shortcut for html code. There is a shortcut for writing out larger blocks of HTML writing out larger blocks of HTML code, or we could just close the code, or we could just close the PHP block and reopen it after the PHP block and reopen it after the HTML is written.HTML is written.

• We copy the correct array to the We copy the correct array to the menu_array when we determine menu_array when we determine what ‘spec’ is. what ‘spec’ is.

display_consistency.php - continueddisplay_consistency.php - continued

// Include function to determine a WFO // Include function to determine a WFO // list for the menu. // list for the menu. // The source code for any function that you// The source code for any function that you // want to use must be included before you call// want to use must be included before you call // the function.// the function. include ("get_wfo_list.php");include ("get_wfo_list.php");

// Select which menu array to display // Select which menu array to display // Arrays may simply be assigned to each// Arrays may simply be assigned to each // other as in the next few lines.// other as in the next few lines. if (substr($spec,0,1) == "D")if (substr($spec,0,1) == "D") $menu_array=$day_list;$menu_array=$day_list; else if (substr($spec,0,1) == "K" || substr($spec,0,2) == else if (substr($spec,0,1) == "K" || substr($spec,0,2) ==

"PA")"PA") $menu_array=get_wfo_list($wfo);$menu_array=get_wfo_list($wfo); elseelse $menu_array=$elementList;$menu_array=$elementList;

Description:Description:

• We copy the correct array to the We copy the correct array to the menu_array when we determine menu_array when we determine what spec is.what spec is.

display_consistency.php – continueddisplay_consistency.php – continued

// Display the menu bar and highlight the// Display the menu bar and highlight the// current WFO/Element/Day // current WFO/Element/Day // the count() function returns the array length// the count() function returns the array lengthecho "<table align=\“center\” boder=\“1\” echo "<table align=\“center\” boder=\“1\”

cols=“ . count($menu_array) . cols=“ . count($menu_array) . "width=\"500\"> \n<TR>";"width=\"500\"> \n<TR>";

for ($i=0; $i<count($menu_array); $i++){ for ($i=0; $i<count($menu_array); $i++){ if ($spec == $menu_array[$i]){if ($spec == $menu_array[$i]){

echo "<td bgcolor=\"red\">";echo "<td bgcolor=\"red\">"; }} else{else{ echo "<td bgcolor=\"#98b1d3\">";echo "<td bgcolor=\"#98b1d3\">"; }} echo "<a href=display_consistency.php?echo "<a href=display_consistency.php?

wfo=“ . $wfo . "&spec=" . wfo=“ . $wfo . "&spec=" . $menu_array[$i] . "> <center><span $menu_array[$i] . "> <center><span class=\"biglinkyellow\">"class=\"biglinkyellow\">"

. $menu_array[$i] . "</span> . $menu_array[$i] . "</span> </center></a></td>\n";</center></a></td>\n";

} } // End of for loop// End of for loop echo "</tr></table><br>\n";echo "</tr></table><br>\n";

Description:Description:

• Now we create a small, one-row table Now we create a small, one-row table to display the “menu”.to display the “menu”.

• We start by using PHP to echo out the We start by using PHP to echo out the table header.table header.

• We then loop through the menu array We then loop through the menu array that we assigned earlier. If the that we assigned earlier. If the current spec is the same as the array current spec is the same as the array element, then it is highlighted so we element, then it is highlighted so we know which Day/WFO/element we are know which Day/WFO/element we are looking at.looking at.

• We create a link for each item in the We create a link for each item in the menu array. The link is the name of menu array. The link is the name of this PHP page, with the spec set to this PHP page, with the spec set to each item in the menu array.each item in the menu array.

• Lastly, we close the row and the table.Lastly, we close the row and the table.

display_consistency.php - continueddisplay_consistency.php - continued

// Include function that reads the consistency file and// Include function that reads the consistency file and// loads the values. // loads the values. include("readConsistencyFile.php");include("readConsistencyFile.php");// Here, we cut some corners for this function. We use// Here, we cut some corners for this function. We use// the wfo name and cycle identifier to specify a file that// the wfo name and cycle identifier to specify a file that// this function reads. Then we return the information// this function reads. Then we return the information// read here to global variables specified in the function.// read here to global variables specified in the function. $status=readConsistencyFile($wfo,$cycle);$status=readConsistencyFile($wfo,$cycle); if($status == -1)if($status == -1) echo "Error on page. Please try again";echo "Error on page. Please try again"; else if ($status == -10)else if ($status == -10) echo "<center><p><h2>No valid data for thisecho "<center><p><h2>No valid data for this

element at this time“ . "</h2></p></center>\n";element at this time“ . "</h2></p></center>\n"; else{else{ echo "<center><span class=\"black\"> Click on echo "<center><span class=\"black\"> Click on each Column each Column

Heading “ . "to Sort by that Item Heading “ . "to Sort by that Item </span></a></center><br>\n";</span></a></center><br>\n"; echo "<center><a echo "<center><a href=\"../supplementalpages/check_explain.php\">"href=\"../supplementalpages/check_explain.php\">" . "Explanation of statistics and methodolgy . "Explanation of statistics and methodolgy

used.</a></center><br>\n";used.</a></center><br>\n"; // Include segment of code to display the sortable tables // Include segment of code to display the sortable tables // This is just a large repetitive section of code that// This is just a large repetitive section of code that // was easier to keep in its own .php file and include it // was easier to keep in its own .php file and include it

// when needed.// when needed. include("HourlyTables.php");include("HourlyTables.php"); }}?> ?> // Close PHP section// Close PHP section

Description:Description:

• We need to use a function to read We need to use a function to read the text file that contains the data. the text file that contains the data. To do this, we need to include the To do this, we need to include the function’s source code.function’s source code.

• Then, we call the function and pass Then, we call the function and pass in two input variables and assign the in two input variables and assign the functions return value to $status for functions return value to $status for error checking.error checking.

• If there is an error, we output a If there is an error, we output a message to the browser.message to the browser.

• Then we write out some basic text Then we write out some basic text to the screen.to the screen.

• The include at the bottom of the The include at the bottom of the page is simply a space-saver. There page is simply a space-saver. There are a number of lines of code, which are a number of lines of code, which are rather repetitive, which I just are rather repetitive, which I just dumped into an include file. dumped into an include file.

display_consistency.php - continueddisplay_consistency.php - continued     <br><center><br><center>    <table border=“2” cellpadding=“5” width="400"><table border=“2” cellpadding=“5” width="400">     <tr><tr>    <td align=“center” bgcolor="#C5D5F3"> <td align=“center” bgcolor="#C5D5F3">

<span class="black"><span class="black">       Table Created:Table Created:

// Display the time that the table was created.// Display the time that the table was created.       <?php<?php         $inc_fname="CONTENTS/" . $region . $inc_fname="CONTENTS/" . $region . "_hourly_stats_time.php";"_hourly_stats_time.php";         include($inc_fname);include($inc_fname);       ?>?>       </span></td></tr></table><br></span></td></tr></table><br>    <!-- End Content area --><!-- End Content area -->    </td></td></tr></tr><tr><tr> <td><td>    <!-- Left Navigation/Spacer Region --><!-- Left Navigation/Spacer Region -->    &nbsp;&nbsp;    <!-- Left Navigation/Spacer Region --><!-- Left Navigation/Spacer Region -->    </td></td>     <td> <td> <!-- Start Footer. This is the standard NDFD footer. --><!-- Start Footer. This is the standard NDFD footer. -->     <?php include'../includes/footer.php'; ?><?php include'../includes/footer.php'; ?> <!-- End Footer --><!-- End Footer -->    </td></td></tr></table></tr></table></body></body></html></html>

Description:Description:

• At the end of the page, we have a At the end of the page, we have a small table that displays the page’s small table that displays the page’s creation time.creation time.

• The PHP script includes the The PHP script includes the “hourly_stats_time.php” file, which just “hourly_stats_time.php” file, which just contains the generation time.contains the generation time.

• At the very end, the “footer.php” file is At the very end, the “footer.php” file is included. This contains the generic included. This contains the generic footer information.footer information.

• We then close the HTML like we We then close the HTML like we normally do. normally do.

Function: readConsistencyFile.phpFunction: readConsistencyFile.php

<?php<?phpfunction readConsistencyFile($wfo_id,$cycle){function readConsistencyFile($wfo_id,$cycle){// These are the global variables alluded to elsewhere.// These are the global variables alluded to elsewhere.global $wfo2, $element, $day, $validTime, $numPts, $thresh, global $wfo2, $element, $day, $validTime, $numPts, $thresh,

$elevDiff, $avgDiff, $bias, $offset1, $offset2, $elevDiff, $avgDiff, $bias, $offset1, $offset2, $mosaicImgName, $discImgName, $mosaicImgName, $discImgName,

$facefile, $element;$facefile, $element;if ($cycle == "hourly"){if ($cycle == "hourly"){ $filename=$cycle . "/" . $wfo_id . "_consistency.php";$filename=$cycle . "/" . $wfo_id . "_consistency.php";}}else{else{ $filename=$cycle . "/" .$wfo_id ."_" .$cycle . $filename=$cycle . "/" .$wfo_id ."_" .$cycle .

"_consistency.html";"_consistency.html";}}

// Once again, this is the same type of format as C. // Once again, this is the same type of format as C. // $fp is a file pointer that is returned with a value of// $fp is a file pointer that is returned with a value of// NULL if the file cannot be opened.// NULL if the file cannot be opened.// Here, we open $filename for reading.// Here, we open $filename for reading.$fp=fopen($filename,"r");$fp=fopen($filename,"r");

if (!$fp)if (!$fp) return -1;return -1;

Description:Description:

• The function declaration is The function declaration is basically the same as C, without basically the same as C, without the variable types. The function the variable types. The function is contained in the {}.is contained in the {}.

• Like other languages, we can Like other languages, we can declare function variables as declare function variables as global, meaning they can be used global, meaning they can be used anywhere else without passing anywhere else without passing them in and out of the function. them in and out of the function. This was done here simply for This was done here simply for convenience. convenience.

readConsistencyFile.php – continuedreadConsistencyFile.php – continued

$i=0;$i=0; // We read from this file until there is no more data// We read from this file until there is no more data while(!feof($fp)){while(!feof($fp)){ if ($cycle == "hourly"){if ($cycle == "hourly"){ // read 5 strings, 1 int, 4 floats, // read 5 strings, 1 int, 4 floats, // 2 ints and 2 strings from this row// 2 ints and 2 strings from this row $line=fscanf($fp,"%s%s%s%s%s%d%f%f%f%f%d%d%s%s");$line=fscanf($fp,"%s%s%s%s%s%d%f%f%f%f%d%d%s%s");

// If we can't read this data, continue to the next iteration// If we can't read this data, continue to the next iteration if ($line == NULL)if ($line == NULL) continue;continue; // list takes line and assigns each value to the specified // list takes line and assigns each value to the specified

variable.variable. // We load the associative array elements for sorting purposes // We load the associative array elements for sorting purposes laterlater

list($wfo2_row, $element_row, $day_row, $ymd_row, $hr_row, list($wfo2_row, $element_row, $day_row, $ymd_row, $hr_row, $numPts["n$i"], $thresh["n$i"], $elevDiff["n$i"], $numPts["n$i"], $thresh["n$i"], $elevDiff["n$i"],

$avgDiff["n$i"], $avgDiff["n$i"], $bias["n$i"], $offset1["n$i"], $offset2["n$i"], $bias["n$i"], $offset1["n$i"], $offset2["n$i"], mosaicImgName["n$i"], $discImgName["n$i"]) = $line;mosaicImgName["n$i"], $discImgName["n$i"]) = $line;

Description:Description:

• We read in data until the end-of-file. We read in data until the end-of-file. The data is fetched, via fscanf into the The data is fetched, via fscanf into the string variable $line. $line is then string variable $line. $line is then broken up into its component broken up into its component variables.variables.

readConsistencyFile.php – continuedreadConsistencyFile.php – continued

// trim removes a trailing comma from each string variable// trim removes a trailing comma from each string variable $wfo2["n$i"]=trim($wfo2_row,",");$wfo2["n$i"]=trim($wfo2_row,","); $day["n$i"]=trim($day_row,",");$day["n$i"]=trim($day_row,","); $element["n$i"]=trim($element_row,",");$element["n$i"]=trim($element_row,","); $validTime["n$i"]=$ymd_row . " " . trim($hr_row,",");$validTime["n$i"]=$ymd_row . " " . trim($hr_row,",");

if ($avgDiff["n$i"] > $thresh["n$i"])if ($avgDiff["n$i"] > $thresh["n$i"]) $facefile["n$i"]="../staticimages/frown.gif";$facefile["n$i"]="../staticimages/frown.gif"; elseelse $facefile["n$i"]="../staticimages/smile.gif";$facefile["n$i"]="../staticimages/smile.gif"; }} else{else{ }} $i++;$i++; }} if ($i == 0){ if ($i == 0){ // didn't read any data, return an error// didn't read any data, return an error code code return -10;return -10; } } fclose($fp);fclose($fp); return 0;return 0;}}?>?>

Description:Description:

• The associative arrays are then The associative arrays are then loaded.loaded.

• After all of the data is read, the file is After all of the data is read, the file is closed and we return to calling routine closed and we return to calling routine

Function: HourlyTables.phpFunction: HourlyTables.php

<table cols=13 border=1 bgcolor="silver"><table cols=13 border=1 bgcolor="silver"><tr bgcolor="DeepSkyBlue"><span class="black"><tr bgcolor="DeepSkyBlue"><span class="black"><?php<?php if($spec[0] != "K" && substr($spec,0,2) != "PA"){if($spec[0] != "K" && substr($spec,0,2) != "PA"){?>?><td><form method="POST" <td><form method="POST"

action="display_consistency.php?cycle=<?action="display_consistency.php?cycle=<?="$cycle"?>&wfo=<?="$wfoName"?="$cycle"?>&wfo=<?="$wfoName"?>&spec=<?="$spec"?>">>&spec=<?="$spec"?>">

<center><input type=submit name="OrderWFO" <center><input type=submit name="OrderWFO" value="WFO"></center></td>value="WFO"></center></td>

<?php<?php }} if ($spec[0] == "K" || substr($spec,0,2) == "PA" || if ($spec[0] == "K" || substr($spec,0,2) == "PA" ||

$spec[0] == "D"){$spec[0] == "D"){?>?> <td><form method="POST" <td><form method="POST"

action="display_consistency.php?cycle=<?action="display_consistency.php?cycle=<?="$cycle"?>&wfo=<?="$wfoName"?="$cycle"?>&wfo=<?="$wfoName"?>&spec=<?="$spec"?>">>&spec=<?="$spec"?>">

<center><input type=submit <center><input type=submit name="OrderElement" name="OrderElement" value="Element"></center></td>value="Element"></center></td>

Description:Description:

• This .php file is included by This .php file is included by display_consistency.php. It contains the display_consistency.php. It contains the sortable table. The sortable table has sortable table. The sortable table has html forms for each column heading that html forms for each column heading that sets the $_POST[“sets the $_POST[“column headingcolumn heading”] ”] variable. Later in the code, the value of variable. Later in the code, the value of each of these variables is checked, and if each of these variables is checked, and if one is not NULL, the table is sorted by one is not NULL, the table is sorted by that column. that column.

• First, we set up the basic HTML table.First, we set up the basic HTML table.

• Then we create the forms for each Then we create the forms for each column heading. It appears simply as column heading. It appears simply as submit button with the column name submit button with the column name inside it.inside it.

• The if statements are used to selectively The if statements are used to selectively output the right headings, since they output the right headings, since they differ depending upon $spec.differ depending upon $spec.

HourlyTables.php – continuedHourlyTables.php – continued

<?php<?php }} if($spec[0] != "D"){if($spec[0] != "D"){?>?> <!-- Notice how we move into and out of php, even in the <!-- Notice how we move into and out of php, even in the

middle of if constructs -->middle of if constructs --> <td><form method="POST" <td><form method="POST"

action="display_consistency.php?cycle=<?="$cycle"?action="display_consistency.php?cycle=<?="$cycle"?>&wfo=<?="$wfoName"?>&spec=<?="$spec"?>">>&wfo=<?="$wfoName"?>&spec=<?="$spec"?>">

<center><input type=submit name="OrderDay" <center><input type=submit name="OrderDay" value="Day"></center></td>value="Day"></center></td>

<?php<?php }}

if ($spec[0] == "K" || substr($spec,0,2) == "PA")if ($spec[0] == "K" || substr($spec,0,2) == "PA") $thisType="wfo2";$thisType="wfo2"; else if($spec[0] == "D")else if($spec[0] == "D") $thisType="day";$thisType="day"; elseelse $thisType="element";$thisType="element"; // The following html block defines the table headlines// The following html block defines the table headlines // as submit buttons on an HTML form. Clicking the // as submit buttons on an HTML form. Clicking the // submit button sorts the data using that column. // submit button sorts the data using that column.

Description:Description:

• This is the same type of operation as This is the same type of operation as the previous slide. Notice that even the previous slide. Notice that even outside of the PHP block, the following outside of the PHP block, the following HTML code is still contained in the PHP HTML code is still contained in the PHP if construct.if construct.

HourlyTables.php - continuedHourlyTables.php - continued

<td><form method="POST" action="display_consistency.php?cycle=<?="$cycle"?>&wfo=<?<td><form method="POST" action="display_consistency.php?cycle=<?="$cycle"?>&wfo=<?="$wfoName"?>&spec=<?="$spec"?>">="$wfoName"?>&spec=<?="$spec"?>">

<center><input type=submit name="OrderTime" value=" Valid Time "></td><center><input type=submit name="OrderTime" value=" Valid Time "></td> <td><form method="POST" action="display_consistency.php?cycle=<?="$cycle"?>&wfo=<?<td><form method="POST" action="display_consistency.php?cycle=<?="$cycle"?>&wfo=<?

="$wfoName"?>&spec=<?="$spec"?>">="$wfoName"?>&spec=<?="$spec"?>"> <center><input type=submit name="OrderFace" value="Approval"></td><center><input type=submit name="OrderFace" value="Approval"></td> <td><form method="POST" action="display_consistency.php?cycle=<?="$cycle"?>&wfo=<?<td><form method="POST" action="display_consistency.php?cycle=<?="$cycle"?>&wfo=<?

="$wfoName"?>&spec=<?="$spec"?>">="$wfoName"?>&spec=<?="$spec"?>"> <center><input type=submit name="OrderNumPts" value="Points"></td><center><input type=submit name="OrderNumPts" value="Points"></td> <td><form method="POST" action="display_consistency.php?cycle=<?="$cycle"?>&wfo=<?<td><form method="POST" action="display_consistency.php?cycle=<?="$cycle"?>&wfo=<?

="$wfoName"?>&spec=<?="$spec"?>">="$wfoName"?>&spec=<?="$spec"?>">

Description:Description:

• This is the remainder of the This is the remainder of the column headings/forms. These column headings/forms. These columns are always present. A columns are always present. A few have omitted…few have omitted…

HourlyTables.php - continuedHourlyTables.php - continued

<?php<?php // Main array that will be sorted // Main array that will be sorted $mainArray = array();$mainArray = array(); if ($_POST["OrderWFO"]){ if ($_POST["OrderWFO"]){

// Now we use the _POST array to access the// Now we use the _POST array to access the// post data sent to this page// post data sent to this page

// Notice that the associative index of the// Notice that the associative index of the// _POST array is the "name" of the HTML form // input statement // _POST array is the "name" of the HTML form // input statement

// array_multisort sorts using two arrays: // array_multisort sorts using two arrays: // wfo and validTime // wfo and validTime

array_multisort($wfo2,$validTime,array_multisort($wfo2,$validTime, SORT_REGULAR);SORT_REGULAR); $mainArray=$wfo2;$mainArray=$wfo2; }} else if ($_POST["OrderElement"]){else if ($_POST["OrderElement"]){ // here we just sort using one asociative array // here we just sort using one asociative array // (meaning each element is refernced by a unique // (meaning each element is refernced by a unique // string rather than a number) // string rather than a number) asort($element,SORT_REGULAR);asort($element,SORT_REGULAR); $mainArray=$element;$mainArray=$element; }}else{else{ asort($facefile,SORT_REGULAR);asort($facefile,SORT_REGULAR); $mainArray=$facefile;$mainArray=$facefile; }}

Description:Description:

• If the $_POST[“”] variable for any of If the $_POST[“”] variable for any of the columns is not NULL, the the columns is not NULL, the main_array is set to that columns main_array is set to that columns array.array.

• The main array is later sorted, re-The main array is later sorted, re-arranging the values in the array, but arranging the values in the array, but also re-arranging the associative also re-arranging the associative indexes of all of the arrays.indexes of all of the arrays.

• Once again, each column heading is Once again, each column heading is tested for, but for simplicity, some are tested for, but for simplicity, some are omitted.omitted.

HourlyTables.php – contHourlyTables.php – cont

$count=0;$count=0; // By having all of these arrays with the same// By having all of these arrays with the same // index names, we can keep all of the corresponding// index names, we can keep all of the corresponding // arrays together even after sorting any one of them.// arrays together even after sorting any one of them. foreach ($mainArray as $key => $val){foreach ($mainArray as $key => $val){ // Select which rows to print out. // Select which rows to print out. if ($thisType == "wfo2"){if ($thisType == "wfo2"){ $comp=$wfo2[$key];$comp=$wfo2[$key]; if ($comp == $spec){if ($comp == $spec){ echo "<tr><span echo "<tr><span

class=\"black\"><center><td>". $element[$key] class=\"black\"><center><td>". $element[$key] ."</td><td> {$day[$key]} </td>"; ."</td><td> {$day[$key]} </td>"; echo "<td> {$validTime[$key]} </td> "echo "<td> {$validTime[$key]} </td> " . "<td align=\"center\"><img src=\"". $facefile[$key] . "<td align=\"center\"><img src=\"". $facefile[$key] . "\" width=\"18\" height=\"18\"></td>". "\" width=\"18\" height=\"18\"></td>" . "<td> {$numPts[$key]} </td><td> ". "<td> {$numPts[$key]} </td><td> " . "{$thresh[$key]} </td><td> {$elevDiff[$key]} </td><td> ". "{$thresh[$key]} </td><td> {$elevDiff[$key]} </td><td> " . "{$avgDiff[$key]} </td><td> {$bias[$key]} </td><td> . "{$avgDiff[$key]} </td><td> {$bias[$key]} </td><td>

{$offset1[$key]} "{$offset1[$key]} " . "</td><td> {$offset2[$key]} </td></center></span></tr>\n";. "</td><td> {$offset2[$key]} </td></center></span></tr>\n"; }} }}

Description:Description:

• We then loop through the main_array. We then loop through the main_array. The foreach construct is a useful way The foreach construct is a useful way to do that.to do that.

• Once again, we need to selectively Once again, we need to selectively print out the data, hence the if print out the data, hence the if ($thisType… statements.($thisType… statements.

• We then output the data in HTML We then output the data in HTML format.format.

HourlyTables.php – continuedHourlyTables.php – continued

else{else{ $comp=$day[$key];$comp=$day[$key]; if ($comp == $spec){if ($comp == $spec){ echo "<tr><span class=\"black\"><center><td>" . $wfo2[$key]echo "<tr><span class=\"black\"><center><td>" . $wfo2[$key]

. " </td><td> {$element[$key]} </td>";. " </td><td> {$element[$key]} </td>"; echo "<td> {$validTime[$key]} </td> “ . "<td align=\"center\"><img src=\"" . $facefile[$key]echo "<td> {$validTime[$key]} </td> “ . "<td align=\"center\"><img src=\"" . $facefile[$key] . "\" width=\"18\" height=\"18\"></td>“ . "<td> {$numPts[$key]} </td><td> ". "\" width=\"18\" height=\"18\"></td>“ . "<td> {$numPts[$key]} </td><td> " . "{$thresh[$key]} </td><td> {$elevDiff[$key]} </td><td> ". "{$thresh[$key]} </td><td> {$elevDiff[$key]} </td><td> " . "{$avgDiff[$key]} </td><td> {$bias[$key]} </td><td> {$offset1[$key]} ". "{$avgDiff[$key]} </td><td> {$bias[$key]} </td><td> {$offset1[$key]} " . "</td><td> {$offset2[$key]} </td></center></span></tr>\n";. "</td><td> {$offset2[$key]} </td></center></span></tr>\n"; }} }} } } ?>?>

</table></table>

Description:Description:

• This outputs the data for the other This outputs the data for the other options, one of which was left out options, one of which was left out in the slide.in the slide.

PHP ResourcesPHP Resources

• PHP.net – PHP.net – http://www.php.net/manual/en/http://www.php.net/manual/en/

– One stop shop for almost any PHP One stop shop for almost any PHP information.information.

• WebMonkey - WebMonkey - http://hotwired.lycos.com/webmonkey/programming/php/http://hotwired.lycos.com/webmonkey/programming/php/

– Contains more specific (and obscure) Contains more specific (and obscure) applications of PHP.applications of PHP.

http://ndfd-dev.weather.gov/forecasts/graphical/MDL_DEMO/display_consistency.php?cycle=hourly&wfo=KLWX&spec=Tdhttp://ndfd-dev.weather.gov/forecasts/graphical/MDL_DEMO/display_consistency.php?cycle=hourly&wfo=KLWX&spec=Td


Recommended