PHP

Revision as of 21:58, 22 April 2015 by Sfws (talk | contribs) (New Page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Using PHP Hypertext Pre-processor

Many web servers support the PHP scripting language, this means that the web server will parse the contents and obey all the script instructions to generate a pure HTML page that it forwards to a user agent (e.g. browser). This Wiki article cannot teach you PHP, nor tell you if your web server supports it, but the examples below give a brief hint of how PHP can be used.

Using PHP templates

This approach incorporates some PHP script in a page that still contains Cumulus web tags so still requires processing by Cumulus. This is suitable when you want to try PHP, but do not want a big redesign of your set-up.

  • The file still contains the HTML shown at the start of this page, indeed it might be a simple edit of a standard cumulus template.
  • It still contains Cumulus web tags, but as it is no longer a standard page, it has to be listed on the Files tab of the Internet screen within the 'Configuration' menu; the remote file name must have the extension ".php" so that the PHP processor will parse the file, before it is passed to the browser as a HTML page.
  • The php script might be used for a decision more complicated than the boolean options that can be achieved just with standard HTML. For example, in the UK vehicle headlights must be used between half-an hour after sunset to half-an-hour before sunrise, so maybe you want to modify a standard "xxxxT.htm" page that is currently processed into "xxxx.htm". Change it to be processed into "xxxx.php" instead and incorporate 2 new chunks of code:
<?php // start of PHP script
     // setting PHP variables using web tags available in Cumulus (version 1.x.x)
                $latestDay	=    '<#metdate format=yyyy>'.'-'; # Year could be treated as integer converted to string, but easier to make it literal
		$latestDay	.=   '<#metdate format=mm>'; # Read as literal so any leading zeros do not make it Octal integer
		$latestDay	.=   '-'.'<#metdate format=dd>'; # Read as literal so any leading zeros do not make it Octal integer
		$sunrise	=	"<#sunrise format=h:nn>"; # use format to remove any leading zero, so can not read it as Octal integer
		$sunset		=	"<#sunset>";
    // setting new PHP variables (not available in Cumulus web tags)
        // end of lighting up time (hours of darkness)
		$darkness_end		=	new DateTime($latestDay.' '.$sunrise); // sunrise
		$darkness_end	->	sub(DateInterval::createFromDateString('30 minutes')); // subtract 30 minutes
	// start of lighting up time (hours of darkness)
		$darkness_start=	new DateTime($latestDay.' '.$sunset); // sunset
		$darkness_start->	add(new DateInterval('P0Y0DT0H30M')); // add 30 minutes
?> <!-- end of PHP script -->

....
 
    <tr>                
	<th title="Darkness times calculated by PHP">Darkness starts</th>
	<td><?php
			echo ($darkness_start->format('g:i a'));
	?></td>
  </tr><tr>
         <th>Darkness ends</th>
         <td><?php
			echo ($darkness_end->format('g:i a'));
	?></td>
  </tr>

Replacing HTML by PHP

  1. A more efficient approach, this is a bigger implementation change, is to set Cumulus to process just something like this file to create PHP variables/arrays from the Cumulus web tags you want to use.
  2. Then you create a new PHP script that does not get processed by Cumulus. For example "thismonth.php" would start with the same HTML5 as shown above, but the table is output by the PHP script:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="<#location> weather data" />
<meta name="keywords" content="Cumulus, <#location> weather data, weather, data, weather station" />
  <title><?php
   include 'cumuluswebtags.php';
   echo "$location"; 
   ?>weather</title>
  <link href="weatherstyle.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="content">
<h1><?php
   echo "$location"; 
   ?> weather
	
....

<table style="width:100%;border-collapse: collapse; border-spacing: 0;" >
   <caption>This month's highs and lows
  <tr>
    <td colspan="3" class="tableseparator_temp">Temperature and Humidity 
  
  <tr class="td_temperature_data">
	<?php
		echo "<th>Highest";
		echo "<td>$MonthTempH $tempunit";
		echo "<td>$MonthTempHT on $MonthTempHD";
		echo "<th>Lowest";
		echo "<td>$MonthTempL $tempunit";
		echo "<td>$MonthTempLT on $MonthTempLD";
	?>
  </tr>

....

echo <<< END_OF_QUOTE
			<tr>
				<td colspan="2" class="spacer">Pressure (sea level)
			
			<tr>
				<th class="labels">Lowest
				<td class="site_data">$MonthPressL {$pressunit}
				<td class="time_stamp">at $MonthPressLT on $MonthPressLD
			
			<tr>
				<th class="labels">Highest
				<td class="site_data">$MonthPressH  {$pressunit}
				<td class="time_stamp">at $MonthPressHT on $MonthPressHD
			
		
END_OF_QUOTE;

....	


Note that only excerpts from the code are shown.  Note the two ways of getting PHP to output HTML code, either by using separate echo commands for each line or using the "heredoc" approach so that one echo can be applied to multiple lines.  In the latter way, the syntax allows a space after echo and after <<< but does not allow any other spaces (or tabs) in the two enclosing lines (that precede and follow all the listed output).