I have about 300 word/pdf files that I want to create a search box to search just that directory and pull up all results on the same page. Could someone please help me with this.
Thanks
Derek McIntire
I have about 300 word/pdf files that I want to create a search box to search just that directory and pull up all results on the same page. Could someone please help me with this.
Thanks
Derek McIntire
Might run a bit slow. Using a database with a precompiled list of words might be beneficial.
The formatting will be a bit off, and the files won't be links or anything, but you should be able to figure that out.PHP Code:<?php
function searchfiles($search,$dir) {
$results = array();
$n=0;
$opendir = opendir($dir);
while ($file = readdir($opendir)) {
if (in_array($file,array('.','..','otherfile.ext','....'))) continue;
//leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory
if (!strpos($search,file_get_contents($dir.'/'.$file))) continue;
$array[$n] = $file;
$n++;
}
return $array;
}
print_r(searchfiles($search,'mydocs')); //set mydocs to your directory
//$search must be the text to match
?>
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Thank You
I will try it and see
where would I put the search box and the submit button in this script?
You would want to do something like this:
Hope this helps.Code:<?php function searchfiles($search,$dir) { $results = array(); $n=0; $opendir = opendir($dir); while ($file = readdir($opendir)) { if (in_array($file,array('.','..','otherfile.ext','....'))) continue; //leave '.' and '..', and add any other files to skip... but try to keep mostly only searched files in this directory if (!strpos($search,file_get_contents($dir.'/'.$file))) continue; $array[$n] = $file; $n++; } return $array; } if (isset($_POST['search'])) { print_r(searchfiles($_POST['term'], 'mydocs')); //set mydocs to your directory //$search must be the text to match } else { ?> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST"> Search Term: <input type="text" name="term"> <input type="submit" name="search"> </form> <?php } ?>
Last edited by thetestingsite; 07-22-2007 at 02:32 AM.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design
I think I know the answer to this ? but If I have php anywhere in my page do I need to save that page with a .php extention
Yes, or at least set up the server to parse whatever file extension you name it to to serve as a PHP file. On most server installs though, you will want to name it as myfile.php (where myfile is the name of the file).
Hope this helps.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design
Thank You for your time
DM
I did everything and I posted it and this what I am recieving on the page that it is susposed to display
Warning: file_get_contents(php): failed to open stream: No such file or directory in /home/content/d/e/r/derekm806/html/ordinances.php on line 69
This is what is on Line 69 of the php file.
if (!strpos($search,file_get_contents($ord.''.$ordinances.php))) continue;
Could you tell me what I am doing wrong.
DM
You took out the slash.
$dir.'/'.$file --> dir/file.ext
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Bookmarks