Jump to content


Photo

PHP Search


  • Please log in to reply
4 replies to this topic

#1 jeeves84

jeeves84
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 23 October 2006 - 11:02 PM

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 http://www.webknowho...archScript.html

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.

#2 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 23 October 2006 - 11:45 PM

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.
<?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.";
  }
} 

?>

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 jeeves84

jeeves84
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 24 October 2006 - 12:18 PM

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

#4 jeeves84

jeeves84
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 24 October 2006 - 11:36 PM

any ideas?

#5 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 25 October 2006 - 04:08 PM

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:

<?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("./");

?>

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.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users