PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
Guts or Glory Poker PHP - A casino-style card game written entirely in PHP


[back]
WB01624_.gif (281 bytes) 01/07/00 "Searching a directory of files" WB01626_.gif (272 bytes)[next]

Compiling the list of files to search through

Before we can create the loop to open each diary entry file, grab the meta tags, and compare to the $search_criteria we need to get the current list of diary entry files. You might remember how we used a regular expression to determine what files were diary entries back when I was building the diary rating script (122399), and a similar technique will be used here. Basically we are going to go through the entire directory and pick out the filenames that are valid and ignore those that aren't.

Firstly though, in order to search through the database of all elligible diary entry pages' meta tags we have an important decision to make. When I'm planning a programming task like this, you might remember, I always try to take the path of least server resources used if it will still fit the task at hand. It really isn't necessary to repeatedly compile the files in the directory when there will only be one new diary added each day. Below is the order of possibilities from slowest (most server resources) to fastest (least server resources used) for this particular task. It is a good idea to make a list like this sometimes, at least mentally if not physically, so that the best choice becomes apparent.

1. Compile the list of diary entry files with each search request DISADVANTAGE: (slowest), ADVANTAGE: Dynamic (real-time), if I add a new diary entry right now, the next person who searches will be able to access this file immediately with no manual entry required by me. This works like the diary entry rating system, if you rate something and then refresh the page, your diary entry rating is dynamically displayed.
2. Compile the list of diary entry files once each day and put in a file. DISADVANTAGE: (extra file reading option required every time a search is done) ADVANTAGE: faster than option #1, and somewhat dynamic because the list will be accurate until I add a new diary entry file. The extra file reading option to fill the array is not that much of a time or resource concern.
3. Add new diary entry file to an array inside the script code. DISADVANTAGE (this is a manual process by me every time I add a new diary entry). ADVANTAGE: Fastest and easiest on the server, the array is already ready to be processed.

I like #1 because it is something I can install and forget about it, but having the server recompile the list every time a search is made is too wasteful when the data isn't going to change only once a day or so. If it was constantly changing then #1 would be the way to go.  #3, although the fastest isn't really a huge performance jump over #2, and I don't like the idea of repeating manual tasks (I have more useful things to do with my time).  So #2 is the option I have chosen. When I add a new diary entry file I want the script to compile all valid diary entries and put them in a file that our search engine can use. We'll call this file "dat_files.txt" and store it in this same directory. First let's analyze the code to open the current directory, read the contents, and print the filename and size of the file:

<?
$diary_directory = opendir(".");
  while($filename = readdir($diary_directory))
{
     print("<br>$filename =<strong> ");
     print(filesize($filename));
     print("</strong>");
}
closedir($diary_directory);
?>

The "." stands for current directory. If we changed that to the actual path of the directory you are interested in searching (in my case the currrent directory is the path where the diary entry files are located) it will work fine. For example:

$diary_directory = opendir("/path/to/www/diary_files");

works the same as "." -- since the period means working directory. Therefore as long as my script is in the same directory that I want to search through, I do not need to include the path.

Next I want to extract the diary entries from the directory and load them into an array. I can now refer back to the code I used when creating my rating script on how to use a regular expression and use similar code to get the proper filenames. By using the explode function we can separate the filenames by the period. Remember that arrays start numbering at zero (I know, I know, I keep saying this, but it is a common mistake). Therefore the $filesplit[1] would be the characters after the period and the $filesplit[0] would be the actual filename minus the extension.

$filesplit = explode(".", $filename);
$check_filename = $filesplit[0];

Now we need to make sure that the filename is comprised of 6 digits from 0-9, by using the same regular expression used in the rating script. If it is, then it is the diary entry filename, irregardless of whether the extension is .html or .php3 or anything else and we need to add it to our $valid_filename array:

if(ereg("[0-9]{6}", $check_filename))
{
   // this is a valid diary entry file because it contains exactly 6 digits
   $valid_filename[] = $check_filename;
}

Ok, now this will go through and pick out all the current diary entry files and load them into the $valid_filename array. Let's do a quick check and make sure this is correct by reviewing the entire code thus far:

<?
$diary_directory = opendir(".");
  while($filename = readdir($diary_directory))
{
    $filesplit = explode(".", $filename);
    $check_filename = $filesplit[0];
       if(ereg("[0-9]{6}", $check_filename))
      {
         $check_filename .= ".$filesplit[1]";
         $valid_filename[] = $check_filename;
      }
}
closedir($diary_directory);
for($index = 0; $index < count($valid_filename); $index++)
{
  print("<br>$valid_filename[$index]);
}
?>

Example #22: searching through a directory and selecting files with a regular expression

Note how I added back to the filename the extension. This will be necessary of course when we try to access the file for searching the meta tags. The last for loop is only to demonstrate how we picked out all the files in the directory that are valid diary dates. Now you should be able to see how easy it would be to pick out files using a regular expression from one of your directories.

Displaying (number of votes) in the rating script after the average rating

I added the option to display the total number of votes so you could see how many votes each diary entry has received thus far in parenthesis following the average rating.  This is setting up one of my future projects where I will display the diary entries by most useful rating first on the diary page. This also demonstrates how if you use the same code for many pages, you can change all pages at one time by altering one file. You can see how the existing average rating code was assembled using cookies by visiting 122399. The code to display the number of votes was pretty simplistic actually:

print(" ($x)");

Please vote on what you think of this diary entry and you should see the count increment by one and the average flucuate accordingly :)

How useful was this diary entry? Avg Surfer Rating: 4.04 (291)

[back]WB01624_.gif (281 bytes) 01/07/00 "Searching a directory of files" WB01626_.gif (272 bytes)[next]

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

Copyright 2000 php-scripts.com Last Modified 07/30/00 06:44