Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

jobs.webdeveloper.com

e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


PHP Flat File Search Engine

Bookmark and Share

by Marc Plotz

July 21, 2009

Need to search through static website content and don't know how? Marc Plotz provides the simple answer.

Introduction

Many developers--myself included--end up going crazy and running for the hills when they are asked to create a static website with full search functionality. Database searching is easy, even when you have to join multiple tables. But how would you create a comprehensive search system for a static website? This is a question that bugged me so much I eventually took a few hours to sit down and write the solution. The complete script can be downloaded HERE. Thanks to WikiPedia for the static content in the provided folders.

Where do we start?

Well, I guess any search engine needs a form. We will keep ours simple:


Click here for larger Code Segment

Like I said, simple. The reason Google is the most-used search engine? Simplicity. I have seen too many complicated search features that ask you to filter by things like Boolean! Now I ask you--what average person using the Internet knows what Boolean means? Besides, if your search algorithm is good enough (like Google's is and I'm not claiming this one is wonderful, but it works) then you don't need pretty forms or filters. At the end of the day the user wants to find something, and as wonderful as your form might be to you, its designer, and as feature rich as it may be on so many levels of coding, it means nothing to the post- office clerk looking for something on your website. If they cant use it, they get bored and move on to a site that is simple.

The form is pretty self explanatory. It submits to the same page that it is found on--so it is always available should the user need to search again, and it will remember the last keywords searched for in the text box. One big thing is that our search uses GET, not POST. The reason for that is, should you require paging at a later stage it is quite simple to incorporate. Pagination with POST values is tricky, and we are going for simplicity here.

Setting it up

First, we need a bit of information about our environment. We let PHP get that for us.

$hits = null;

$full_url = $_SERVER['PHP_SELF'];
$site_url = eregi_replace('index.php', '', $full_url);
$directory_list = array('data', 'moredata');

First, we clear the hits variable, the reason for this is that the same page loads after every search and we do not want previous values messing with current values. Next, we get the value for the site_url, just so that it is dynamic and we can chuck this script in everywhere without having to change values every time. The directory_list variable is an array containing the list of directories you would like to search through. In this instance the directories are in our web root, although we could map these as "content/data", etcetera. Our search engine is going to search through every file in these directories and look for a result.

foreach($directory_list as $dirlist)

{
    $directory_url = $site_url.$dirlist."/";
    $getDirectory = opendir($dirlist);

    while($dirName = readdir($getDirectory))
    {
        $getdirArray[] = $dirName;
    }

    closedir($getDirectory);

    $dirCount = count($getdirArray);
    sort($getdirArray);

At last the interesting stuff begins. We open each directory one at a time and get a count of how many directories there are, as well as the directory names.

    for($dir=0; $dir < $dirCount; $dir++)

    {
        if (substr($getdirArray[$dir], 0, 1) != ".")
        {
            $label = eregi_replace('_', ' ', $getdirArray[$dir]);

            $directory = $dirlist.'/'.$getdirArray[$dir]."/";
            $complete_url = $site_url.$directory;

            if(is_dir($directory))
            {
               $myDirectory = opendir($directory);

                $dirArray = null;

                while($entryName = readdir($myDirectory))
                    $dirArray[] = $entryName;

                closedir($myDirectory);

                $indexCount = count($dirArray);
                sort($dirArray);
            }

PHP Flat File Search Engine
PHP Flat File Search Engine - Cont.


Up to => Home / Authoring / Tutorials / PHP




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers