PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

[back]WB01624_.gif (281 bytes) 12/26/99 "Reading And Using Files" WB01626_.gif (272 bytes)[next]

Reading And Using Files

Following up on averages in my last diary entry, I would like to display the average rating for each diary page so future readers of these diary entries will be able to navigate to the surfer-rated most useful diary entries first. It will be best in the long run to have a separate directory of all the .dat files so that we can use PHP to more easily navigate through the data files (this may make more sense to you later) and not have all the .html files and diary files mixed in. Let's create a directory called /data and use that for all our data files. Again, we need to make the permissions for the new data directory amenable so we can read, write and create new files (chmod 777) or you will get a file permission error while trying to write to the directory. Now we need to modify the file writing lines in the voting script to give the path the new directory.

Change the following line of code:

$newrate = fopen($filename, "a");

to read like this:

$newrate = fopen("$path/$filename", "a");

Now declare your $path to your newly created data directory at the beginning of the script:

$path = "/home/username/www/votedir/data";

Obviously you need to modify the above variable to fit the path on your server, but this illustrates how easy it is to point outside of the working directory. By default if you use a filename with no path, PHP will write to the default directory. When you will be creating many data files remember that it is easier to create a separate data directory to keep your files better organized.

Ok, now let's create the code to pull the current data out of the correct vote file for the day and display the average dynamically on each page next to our "rate" submit button.

<?
$path = "/home/usr/www/votedir/data";
  $x= -1;
    if($file = fopen("$path/$filename", "r"))
   {
      while(!feof($file))
     {
      $therate = fgetss($file, 255);
      $x++;
      $count = $count + $therate;
     }
    fclose($file);
   }
$average = ($count / $x);
print("$average);
?>

A better way might be to make the above a function call, and pass the filename to it as an argument. The code to make the above a function is:

<?
function average($filename)
{
  $path =
"/home/usr/www/votedir/data";
  $x= -1;
    if($file = fopen("$path/$filename", "r"))
   {
      while(!feof($file))
     {
      $therate = fgetss($file, 255);
      $x++;
      $count = $count + $therate;
     }
    fclose($file);
   }
$average = ($count / $x);
print($average);
}
?>

Now we have seen "a" which was for appending, or adding data onto a file, and the "r" is used to read the contents of a file. The $x variable is being used as a counter inside the while loop to determine how many entries are actually in the file. feof is the function to read the file in a stream until the end of feed (or last line of data, whichever you prefer) is reached. Remember that the ! means not equal to, so we are "reading each line of the file until the end of feed is reached."  Let's put average.php3 into a separate file and include it along with our daily background changing now. So the code at the top of each HTML page will look like this now:

<?
include("average.php3");
include("body_change.php3");
?>

Now on the HTML page where I want to print the voting average all I have to do is insert the following (only replacing the filename between the quotes with the correct filename I want the average calculated from:

<?
average("122699.dat");
?>

Example #11: Showing the average surfer rating for 12/24/99

I want to clean up the number of decimal places that are displayed. This can be done using the printf function. Here are a few examples of ways to alter the output:

printf("%.3f", $average); 4.292
printf("%.2f", $average);   4.29
printf("%.1f", $average);   4.2
printf("%.0f", $average);   4   <--- shows only integers

See the php manual for more on the printf function.

Building in Error Protection

We still need to build in some error protection in case there are no votes yet, thus the file hasn't been created yet. That would cause a parsing error because we'd be trying to open a file that didn't exist yet. We use the file_exists function to do this.

if(file_exists("$path/$filename"))
{
    print("the file exists");
}
  else
{
    print("file doesn't exist yet");
}

Please vote on what you think of this diary lesson :)

How useful did you find this diary entry? Avg Surfer Rating: 3.83 (511)

[back]WB01624_.gif (281 bytes) 12/26/99 "Reading And Using Files" WB01626_.gif (272 bytes)[next]

PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

Copyright 2000 php-scripts.com Last Modified 02/9/04 11:30