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
https://forums.phpfreaks.com/topic/24885-php-search/
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
https://forums.phpfreaks.com/topic/24885-php-search/#findComment-113424
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
https://forums.phpfreaks.com/topic/24885-php-search/#findComment-114270
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.