jeeves84 Posted October 23, 2006 Share Posted October 23, 2006 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 23, 2006 Share Posted October 23, 2006 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] Quote Link to comment Share on other sites More sharing options...
jeeves84 Posted October 24, 2006 Author Share Posted October 24, 2006 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 Quote Link to comment Share on other sites More sharing options...
jeeves84 Posted October 24, 2006 Author Share Posted October 24, 2006 any ideas? Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 25, 2006 Share Posted October 25, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.