Linux.com

Feature: PHP

Create a data dashboard with PHP and a browser

By Rob Reilly on November 08, 2004 (8:00:00 AM)

Share    Print    Comments   

How many times have you needed a quick and dirty method of displaying data, in a "dashboard" format, that everybody could easily view? Everybody has a Web browser, so why not use that for your dashboard? PHP and Apache make it easy. Here's how you can take a text file, evaluate the content using PHP, then display results on a browser dashboard.

You'll need a networked Linux box with Apache and PHP loaded as your data collection server. My server is on old 200MHz Pentium desktop box with 128MB of memory and SUSE Linux 8.2 Pro installed. A recycled desktop machine can easily serve dashboard pages for 20 or 30 people.

I made sure that I had SSH loaded so that the box could run headless and keyboard-less once everything was set up. SSH, with X Window forwarding enabled, lets you log in and edit your scripts with GUI-based editors.

I'm not going to go into the security issues of running an Apache Web server on the Internet here, but you should use appropriate security measures for your situation. Since I was using my server behind a firewall on a small network, I simply changed the Web directory to allow write permission for myself. As root, I created a php directory under /www and chowned it to rreilly (myself as the user). I then chmoded the permissions of the /www/php directory to 664 -- quick and dirty.

The client side was a hundred times easier. All I needed was a current browser on a network connected machine. I had a laptop running SUSE Linux and Mozilla, and an old Windows 98 machine also running Mozilla (Windows version).

If you are going to edit your scripts and view your dashboard from Windows clients, I'd suggest that you load Putty so you can log into the server using SSH. You can then use vi to edit your PHP scripts.

Read a file and browse it

The basis for this whole exercise was being able to read a text file and do some operations on the data. To begin, create the following program on the server running Apache and PHP and save it as widgetproj1.php in your /www/php directory (or whatever Apache root directory you are using). Take care -- you'll chase your tail for a while if you mistakenly name it with a .htm extension:

<HTML>
<HEAD></HEAD>
<BODY>

<?PHP

$filename = "/www/php/widgetdata.txt" ;
$filepointer = fopen($filename, "r");
$array = file($filename);
fclose($filepointer);

print ("<strong>Web-based Widget Dashboard - $filename</strong><br><br>");

for ($n=0; $n < count($array); $n++) {
      print("$array[$n] <br>");
      }
?>
</BODY>
</HTML>

In the same directory, create a file called widgetdata.txt with this data:

5  10/23/04 10:24 am
10 10/23/04 11:04 am
3  10/23/04 03:30 pm
13 10/24/04 09:45 am
27 10/25/04 04:30 pm
15 10/26/04 02:10 pm
7  10/27/04 06:37 am

Now that you have a sample PHP script and a data file, let's see how it works.

Figure 1

Use a networked browser and type in the script's URL -- e.g. http://192.168.3.13/php/widgetproj1.php. The script opens the /www/php/widgetdata.txt file for reading and assigns it a file pointer. The file command assigns a pointer to the $array. We then print a heading, then step through the array with a for loop, printing each line. The data simply appears verbatim, with a bold title listing the file name in the browser window, as shown here.

A basic dashboard

But we can do better. I modified the script to show a color-coded square, indicating whether my widgets were below, at, or above an arbitrary value of 10. Here's the new code that I saved as /www/php/widgetproj2.php:

<HTML>
<HEAD></HEAD>
<BODY>

<?PHP

$filename = "/www/php/widgetdata.txt";
$filepointer = fopen($filename, "r");
$array = file($filename);
fclose($filepointer);

print ("<strong>Web Based Widget Dashboard - $filename</strong><br><br>");
print ("<u>Widget Production Color Code <br></u>");

print ("Below 10 widgets per hr. = <font color=\"#FF1221\">Red</font><br>");
print ("10 widgets per hr. = <font color=\"#2D13FF\">Blue</font><br>");
print ("Exceeding 10 widgets per hr. = <font color=\"#13FF07\">Green</font><br><br>");

print("<u>Code Widgets  Date    Time</u><br><br>");

for ($n=0; $n < count($array); $n++) {
      $string = $array[$n];
      $widgets = substr($string,0, 2);
      $date = substr($string,3, 9);
      $time = substr($string,11,19);

      if ($widgets < 10) {
          print("<img src=\"redbutton.jpg\" width=\"25\" height=\"26\">");
      }

      elseif ($widgets == 10) {
          print("<img src=\"bluebutton.jpg\" width=\"25\" height=\"26\"> ");
      }

      else {
          print("<img src=\"greenbutton.jpg\" width=\"25\" height=\"26\"> ");
      }

      print("&nbsp;&nbsp;&nbsp;&nbsp; $widgets &nbsp;&nbsp;&nbsp;&nbsp; $date &nbsp;&nbsp; $time <br>");
}

?>

</BODY>
</HTML>

First I added some lines of text at the beginning to give context to the dashboard. I added the substr() function to break up each line of data into values that could be tested. I then added if, else, and elseif statements that printed color-coded JPG files according to my conditions. You could easily use any other type of graphic, button, or icon. If the value was less than 10, output a red square. If the value was 10, output a blue square. Finally, if the value was greater than 10, output a green square. The actual data was then printed after each square and prior to the next loop.

Figure 2

In my browser I typed http://192.168.3.13/php/widgetproj2.php and was greeted with colored squares denoting the various levels.

There's one area that might cause you problems. Make sure to "esc" the double quotes around the values in the print statements; that is, use a \ in front of the double quotes. Otherwise, you'll get a crazy "unexpected T_STRING" error that you'll never figure out.

What's next

You can make your dashboard even fancier by connecting to an SQL database. Other possibilities include sending data from shop floor machines or sensors over a serial line and writing it into a file. Then every time the Web page is reloaded, new data could be displayed.

You could set up simple tracking systems with data created using cron and standard shell tools like ls, grep, and awk. For that matter, you could even input your data with vi or any other text editor. Remember, quick and dirty to get the job done.

By carefully boiling your data down into dashboards that give high level go/no-go results, you won't spend valuable time interpreting numbers and figures. I wish I had had PHP, Apache, and Linux 10 years ago when I was doing metrics and project reporting in corporate America. It would have made my life considerably easier.

Rob Reilly is a consultant who specializes in helping clients communicate effectively. Many of his published articles explain the use of Linux, portable computing, and presentation technology, especially as how it relates to communication in business. His stories appear in various high-end Linux and business media outlets.

Share    Print    Comments   

Comments

on Create a data dashboard with PHP and a browser

Note: Comments are owned by the poster. We are not responsible for their content.

Compile Excel Spreadsheets into Apache/PHP

Posted by: Anonymous Coward on November 08, 2004 11:50 PM

Along these lines...



The Jedox Worksheet Server (see jedox.com) compiles excel workbooks into Apache/PHP web charts and sheets. THe result is a multi-user secure web application that anyone who can jockey a spreadsheet can design and implement.



The data lives on the server and can be updated by anyone with a web browser and the proper login. No excel license needed (except for the designer).



It also talks to ODBC-compliant databases.



Business users can get data instantly from field offices, sales reps, vendors, other departments, the shop floor, whatever.



Each branch office sees only their own data and charts, central office sees the overall summary (and whatever else they want).



Something like this could end the need to consolidate and email spreadsheets around, a too-common business practice today.



I'm Just a happy user always looking for ways to use OSS. I donlt work for Jedox.

#

Re:Compile Excel Spreadsheets into Apache/PHP

Posted by: Anonymous Coward on November 09, 2004 12:32 AM
I donlt work for Jedox.


Yeah..... Riiiiiight....

#

Excel output is a php addon any site can do it

Posted by: Anonymous Coward on November 09, 2004 09:39 AM
Excel from Perl I have done in the past. So not a big feature. Mind you a database is required to do it cheep apgDB2xls will get the output bit just building the Perl input.

Note this is the point most things are not big new features. Just new features to PHP in time they will become free.

Dont forget xlhtml ie xls to a html webpage not a big feature at all.

#

Insecure approach

Posted by: derekfountain on November 09, 2004 10:49 AM
This is pretty basic stuff, and I think the reality is that the world isn't this simple anymore. The approach used is hopelessly prone to XSS attacks (and worse, like PHP remote inclusion attacks) unless the data file is carefully secured and its contents controlled. I wouldn't recommend this technique for anything but the most basic, trusted intranet, and certainly not for anything which faces the Internet.


BTW, you don't need to open a file in PHP in order to use the file() function.

#

Re:Insecure approach

Posted by: Anonymous Coward on November 10, 2004 05:29 PM
So how can this be made secure against XSS or PHP remote inclusion attacks, or what other technique would you use?

#

What interest has this article at all????

Posted by: Anonymous Coward on November 09, 2004 07:41 PM

This guy really seems one who has just discovered programming and php. What he describes is what I would put as an exercise to my 16 year old pupils on the first day of a PHP programming course, just to get a feeling of PHP.


I really don't think this article is interesting at all for anyone but someone who is an absolute beginner in web programming.

#

Re:What interest has this article at all????

Posted by: Preston St. Pierre on November 09, 2004 10:05 PM
So let the beginners read it. Why complain? Is it hurting you that the article is up?

#

Re:What interest has this article at all????

Posted by: Anonymous Coward on November 10, 2004 01:26 AM
I don't expect this type of article to show in a technical news magazine. Mainly because it's no news at all. It's a beginner's tale.

#

Re:What interest has this article at all????

Posted by: Anonymous Coward on November 12, 2004 02:13 AM
I was a little disappointed as well - the title raised my expectations too high. But if you look at it as musings on making data available via web services, it could serve as the start of an interesting article.

I would like to see a better discussion of the security threats involved in this approach (and since it is such a simple approach, identifying the vulnerabilities shouldn't take that much effort).

I'd also like to see a comparison of approaches. What's presented could, it seems, be done equally well with a XSL stylesheet approach. I think both approaches have their place and it would be nice to compare them for some simple cases.

Russell.

#

Complete n00b

Posted by: Anonymous Coward on November 17, 2004 01:03 AM
<TT><HTML>
<HEAD></HEAD>
<BODY&gt<nobr>;<wbr></nobr>

 
<?PHP

 
$filenam e = "/www/php/widgetdata.txt";
$filepointer = fopen($filename, "r");
$array = file($filename);
fclose($filepointer);
?>
<str<nobr>o<wbr></nobr> ng>Web Based Widget Dashboard - $filename</strong><br><br\>
<u>Widge<nobr>t<wbr></nobr> Production Color Code <br></u>

 
Below 10 widgets per hr. = <font color=\"#FF1221\">Red</font><br>
<nobr>1<wbr></nobr> 0 widgets per hr. = <font color=\"#2D13FF\">Blue</font><br>
Exceedin<nobr>g<wbr></nobr> 10 widgets per hr. = <font color=\"#13FF07\">Green</font><br><br>
<u>Cod<nobr>e<wbr></nobr> Widgets  Date    Time</u><br><br>

 
<?php
// or alternatively:

 
echo "Below 10 widgets per hr. = <font color=\"#FF1221\">Red</font><br>".
"<nobr>1<wbr></nobr> 0 widgets per hr. = <font color=\"#2D13FF\">Blue</font><br>".
"Exceedin<nobr>g<wbr></nobr> 10 widgets per hr. = <font color=\"#13FF07\">Green</font><br><br>".
"<u>Cod<nobr>e<wbr></nobr> Widgets  Date    Time</u><br><br>";

 
foreach ($array as $string) {
      list($string,$widgets,$date,$time) = explode(" ", $string)
      if ($widgets < 10) {
          $color='red';
      }

 
      elseif ($widgets == 10) {
          $color='blue';
      }

 
      else {
          $color='green';
      }
      echo "<img src=\"$string.jpg\" width=\"25\" height=\"26\"> ";
      print("&nbsp;&nbsp;&nbsp;&nbsp; $widgets &nbsp;&nbsp;&nbsp;&nbsp; $date &nbsp;&nbsp; $time <br>");
}

 
?>

 
</BODY>
</HTML&gt<nobr> <wbr></nobr>;</TT>
Which would you rather maintain?

#

Re:Complete n00b

Posted by: Anonymous Coward on November 17, 2004 01:06 AM
Hrm, some things didn't get pasted right on my reply.

But the one error i made is:
<TT>  echo "<img src=\"$string.jpg\" width=\"25\" height=\"26\"> ";</TT>
should be:
<TT>  echo "<img src=\"$color.jpg\" width=\"25\" height=\"26\"> ";</TT>
Or even better yet (less escapes, increases readility):
<TT>  echo '<img src="'.$color.'.jpg" width="25" height="26">';</TT>

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya