Jump to content

PHP Search


jeeves84

Recommended Posts

I'm teaching myself HTML and PHP, and find these forums and others to be a big help. I am trying to add a search facility to a site, using a tutorial i found here [url=http://www.webknowhow.net/dir/Other_Resources/articles/PHPSearchScript.html]http://www.webknowhow.net/dir/Other_Resources/articles/PHPSearchScript.html[/url]

It works reasonably well, but I would like to customise it slightly. Firstly, I would like the results returned to only include .html or .php files, as it currently lists image files in the results.

I would also like, if possible, it to display "no results found" rather than a blank screen.

Any help would be greatly appreciated. Cheers.
Link to comment
Share on other sites

This should get you started. Note: You do not need to pass a $count variable to the function. it will be given a value of 0 the first time it is called and then will be used within the function if subfolders are searched.
[code]<?php

function listFiles($dir){
  $handle=opendir($dir; $count=0);
  while(false!==($file=readdir($handle))){
    if($file!="."&&$file!=".."){

      //if it is a directory, then continue
      if(is_dir("$dir/$file")){
        listFiles("$dir/$file"; $count);
      }
      else{
        //Find the extension
        $fileExt = strrchr($file, ".")
        if ($fileExt==".html" || $fileExt==".php") {
          $count++;
          echo $file . "<br>";
        }
      }
    }
  }
  if ($count == 0) {
    echo "No files found.";
  }
}

?>[/code]
Link to comment
Share on other sites

Hi, stupid question i know, but is your script supposed to replace the existing one or be added to it. I tried using your one but got
"Parse error: parse error, unexpected ';' in /homepages/21/d145981504/htdocs/search.php on line 133"
Thanks for your help
Link to comment
Share on other sites

The code I posted was not tested, just a quick rewite. There are a couple of formatting erros and the $count feature does not work properly. I have corrected it:

[code]<?php

$fileCount = 0;
function listFiles($dir){
  global $fileCount;
  $handle=opendir($dir);
  while(false!==($file=readdir($handle))){
    if($file!="." && $file!=".."){

      //if it is a directory, then continue
      if(is_dir("$dir/$file")){
        listFiles("$dir/$file");
      }
      else{
        //Find the extension
        $fileExt = strrchr($file, ".");
        if ($fileExt==".html" || $fileExt==".php") {
          $fileCount++;
          echo $file . "<br>";
        }
      }
    }
  }
  if ($fileCount == 0) {
    echo "No results found.";
  }
}

listFiles("./");

?>[/code]

I image you would replace the other script with this one and make any modifications you need as far as how the data is displayed. This is just a very simplistic script. personally I would tweak it to show folders and indent the data accordingly.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.