One of the strengths of PHP is the way that it makes it easy to do the sort of things that you want to do. In this example we read and process data from a file on the server.
A text file footsie.txt contains the average level of the FT Share Index over the past 6 months. footsie.php reads the file and produces HTML to display a bar graph of the figures.
Let us start by looking at the HTML code that generates those variable length green lines. Here's the HTML that footsie.php generated for May and June:
<tr> <td class="month">May</td> <td class="ave" align="right">6200</td> <td><img src="greendot.gif" height="20" width="124" /></td> </tr> <tr> <td class="month">June</td> <td class="ave" align="right">6425</td> <td><img src="greendot.gif" height="20" width="349" /></td> </tr>
The green lines are created by including an image, greendot.gif, that is just a single green pixel. It is stretched to the appropriate size by the width and height attributes of the <img> tag
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/tr/xhtml1/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb"> <head> <title>footsie.php</title> <style type="text/css"> .month { background:#99FFFF } .ave { background:#CC99FF } </style> </head> <body> <h1>footsie 100 averages - last 6 months</h1> <?php // load the file of months and averages into the array $arr = file("footsie.txt"); // find lowest value to scale graphics $low = $arr[1]; for ($i = 1; $i<count($arr); $i += 2) { $arr[$i] = rtrim($arr[$i]); if ($arr[$i] < $low) { $low = $arr[$i]; } } $base = $low * .98; ?> <table width="75%" border="0" cellspacing="5" cellpadding="5"> <tr> <th class="month">Month</th> <th class="ave">Average</th> <td></td> </tr> <?php // loop through the array outputting the month, average and a graphic // (scaled by height and width) to represent the value for ($i = 0; $i<count($arr); $i += 2) { ?> <tr> <td class="month"><?php echo rtrim($arr[$i]) ?></td> <td class="ave" align="right"><?php echo ($arr[$i + 1]) ?></td> <td><img src="greendot.gif" height="20" width="<?php echo ($arr[$i + 1] - $base) ?>" alt="<?php echo $arr[$i + 1] ?>"/></td> </tr> <?php } ?> </table> </body></html>
This requires some explanation:
PHP provides many file handling functions; e.g. copy(), file_exists(), mkdir() and plenty more.
file() is very useful - it reads a whole file and returns an array with each line in the file as a separate item in the array so...
$arr = file("footsie.txt");
reads footsie.txt leaving $arr containing...
$arr[0] = May $arr[1] = 6200 $arr[2] = June $arr[3] = 6425
But the newline characters are still there waiting to be chomped. In order to scale the bars of the graph, the program looks for the lowest value in the array.
$low = $arr[1]; for ($i = 1; $i<count($arr); $i += 2) { $arr[$i] = rtrim($arr[$i]); if ($arr[$i] < $low) { $low = $arr[$i]; } } $base = $low * .98;
Here the function count() returns the length of the array $arr.
Note that only alternate array values are examined - why?
rtrim() truncates trailing whitespace chomping off the newline characters.
Another loop generates the rows in the HTML table.
for ($i = 0; $i<count($arr); $i += 2) { ?> <tr> <td class="month"><?php echo rtrim($arr[$i]) ?></td> <td class="ave" align="right"><?php echo ($arr[$i + 1]) ?></td> <td><img src="greendot.gif" height="20" width="<?php echo ($arr[$i + 1] - $base) ?>" alt="<?php echo $arr[$i + 1] ?>"/></td> </tr> <?php } ?>
Is this a reasonable way to scale the data onto the graph?