Jump to content

Archived

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

jeeves84

PHP Search

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.

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.