Dynadot โ€” .com Registration $8.99

"Search site" script needed

Spaceship Spaceship
Watch

Gene

Gene PimentelTop Member
Impact
485
I'm having a difficult finding a real simple SEARCH script for a product web site. I simply need to allow customers to enter a search word, and the script should look through every html page within one directory of the site (which contains possibly 200 small html pages). Then post a list of the pages that contain their keyword.

Any recommendations?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Just to clarify Gene...you want a script that looks through html pages in a certain directory to find the search word and then display a list or what exactly?
 
0
•••
netzilla said:
Just to clarify Gene...you want a script that looks through html pages in a certain directory to find the search word and then display a list or what exactly?

It should display a list of the pages that contain their keyword. Each of the pages in that directory are for one specific product. So if they type in the keyword "fudge", it should show them a list of product pages that have "fudge" in the product description.
 
0
•••
Gene, are you looking for something like THIS ?.
 
0
•••
I can't say that I know one off hand but I would be willing to put a script together with this functionality if you wish. All I would need is a listing of the html page names and the code from one of them (as long as they are coded similiar).

I will not charge you for this buy may ask for some rep if you dont mind. Let me know.
 
0
•••
virgil said:
Gene, are you looking for something like THIS ?.

Thanks Virgil, but that seems like overkill. I don't want a "Search Engine" looking page. I just want to place a tiny search box on the page, kinda like the search box at the top of this NamePros site.

netzilla said:
I can't say that I know one off hand but I would be willing to put a script together with this functionality if you wish. All I would need is a listing of the html page names and the code from one of them (as long as they are coded similiar).

I will not charge you for this buy may ask for some rep if you dont mind. Let me know.

That's very kind of you! I'm wondering if this will work though... if it's required that I give you a list of the page file names, what happens when pages are added or deleted? The list of pages will likely change frequently. Can this be created so the search simply looks at all the files in a specific folder?
 
0
•••
Actually yes it can I can do it that way. Specify the name of the directory you would like to use and send me a copy of the html code you are using and I will proceed from there.
 
0
•••
I'll have this done in about 2 mins - i'll edit the code in.

--edit--

usage: search_dir("directory","criteria","extensions to exclude");
note: directory name must NOT end with a trailing slash.
output: returns an array of filenames of pages that contain the criteria.

PHP:
function get_files($dir,$q,$ext)
{
     $directory = opendir($dir);
     foreach($directory as file){
          if(!is_dir($dir.'/',$file)){
               $banned_ext = 0;
               $ext2 = explode(',',$ext);
               $contents=file_get_contents($dir.'/',$file);
               foreach($ext2 as $extension){
                    if(eregi($extension,$file){
                         $banned_ext = 1;
                    }
               }
               if($banned_ext == 0&&eregi($q,$contents)){
                    if(!empty($contents)){
                         array_push($contents,$file);
                    }else{
                         $contents[0] = $file
                    }
               }
          }
     }
     return $contents;
}
 
Last edited:
0
•••
netzilla said:
Actually yes it can I can do it that way. Specify the name of the directory you would like to use and send me a copy of the html code you are using and I will proceed from there.

I've sent you the sample file via PM

.
.
.

Mikor said:
I'll have this done in about 2 mins - i'll edit the code in.

--edit--

usage: search_dir("directory","criteria","extensions to exclude");
note: directory name must NOT end with a trailing slash.
output: returns an array of filenames of pages that contain the criteria.

PHP:
function get_files($dir,$q,$ext)
{
     $directory = opendir($dir);
     foreach($directory as file){
          if(!is_dir($dir.'/',$file)){
               $banned_ext = 0;
               $ext2 = explode(',',$ext);
               $contents=file_get_contents($dir.'/',$file);
               foreach($ext2 as $extension){
                    if(eregi($extension,$file){
                         $banned_ext = 1;
                    }
               }
               if($banned_ext == 0&&eregi($q,$contents)){
                    if(!empty($contents)){
                         array_push($contents,$file);
                    }else{
                         $contents[0] = $file
                    }
               }
          }
     }
     return $contents;
}
Thank you! I'll test this out and see if it does what I hope it will. Appreciate it!
 
Last edited:
0
•••
Way to butt in Mikor :td:
 
0
•••
netzilla said:
Way to butt in Mikor :td:

He may use your script, some people like to see a variety of scripts before they decide which one to use.
 
0
•••
Mikor said:
He may use your script, some people like to see a variety of scripts before they decide which one to use.

Exactly. I appreciate both of your submissions... and more are welcome. I will be glad to add rep points to all who participate.
 
0
•••
netzilla: Have you come up with anything I can try?

Mikor: thanks for your help so far, but your script is not working at all. I've PM'ed you the link where it is installed if you want to have a look.

I still have not found a workable solution. If anybody else has any suggestions, I'm all ears!
 
0
•••
Sorry Gene I got side tracked last night. I will have your script finished shortly.
 
0
•••
Have you cosnidered using Google site search? I recently set it up on a website and it works perfectly and also gives you a chance for a commission.
 
0
•••
Gene - I have a working script running. I made it so that when the click the file name it takes them to that page. Just make sure your search box is named search. Please let me know once you tested it.

Code:
<?php

// Directory name
$dir = "includes";

// Search term posted from form
$search = $_POST['search'];

// Opens directory
$open = opendir($dir);

// Checks files for search term
while (($file = readdir($open)) !== false) {
  if (($file !== ".") && ($file !== "..")) {
    $xfile = "{$dir}/{$file}";
    $fp = fopen($xfile, r);
    $contents = fread($fp, filesize($xfile));
    $close = fclose($fp);
    if (strstr($contents, $search)!== false) {
    echo "<a href=$xfile>$file</a><br>";
    }
  }   
}

// Closes directory
$close = closedir($open);
?>
 
Last edited:
0
•••
::: sigh ::: maybe I'm doing something wrong, but I just can't get this to work.

I created a file "results.php" containing exactly your code below. I created a simple search box with POST to results.php.

No matter what keyword I type in, the page comes up blank.

netzilla said:
Gene - I have a working script running. I made it so that when the click the file name it takes them to that page. Just make sure your search box is named search. Please let me know once you tested it.

Code:
<?php

// Directory name
$dir = "includes";

// Search term posted from form
$search = $_POST['search'];

// Opens directory
$open = opendir($dir);

// Checks files for search term
while (($file = readdir($open)) !== false) {
  if (($file !== ".") && ($file !== "..")) {
    $xfile = "{$dir}/{$file}";
    $fp = fopen($xfile, r);
    $contents = fread($fp, filesize($xfile));
    $close = fclose($fp);
    if (strstr($contents, $search)!== false) {
    echo "<a href=$xfile>$file</a><br>";
    }
  }   
}

// Closes directory
$close = closedir($open);
?>

Where can I find this?

BetterHosting said:
Have you cosnidered using Google site search? I recently set it up on a website and it works perfectly and also gives you a chance for a commission.
 
0
•••
Gene - Please make sure this file is just outside of the includes directory. Also, let me see the code for the page containing the search code please. I bet its just one simple variable don't worry we can get it going. It worked on my dev server so again its just probably something simple.
 
Last edited:
0
•••
Where can I find this?

Go to http://www.google.com/adsense and if you don't already have an account sign up for one. After you are logged in to you account click "AdSense Setup." Then, click "AdSense for Search." There are various options for you to go through. Make sure to click the one that says "Google WebSearch + SiteSearch" and enter your URL. And, at the bottom of the page select "Open results within my own site" and enter the url for your results page (for instance mine is http://poshgear.thatposhgirl.com/search.html). Once you've entered the information and decided how you want everything to look click "Continue." You will get two snippets of code. The first textarea contains the code for your search box and the second textarea contains the code for your results page.

Create a file named search.html (or whatever you decided to call your results page) and paste the second snippet of code into it. Place the snippet of code for the search box wherever you want the search box to be. It should work.

You can test it on my site: http://poshgear.thatposhgirl.com
Select the radial that says "poshgear.thatposhgirl.com" and enter a search term ("bush" will get lots of results). You can click on the links that come up for my site pages but don't click on any ads because I don't want to get in trouble.
 
0
•••
netzilla said:
Gene - Please make sure this file is just outside of the includes directory. Also, let me see the code for the page containing the search code please. I bet its just one simple variable don't worry we can get it going. It worked on my dev server so again its just probably something simple.

We're almost there! It is working now... I had an error in the search page. It's now fixed.

Now -- to make it more user friendly, can we have it list a DESCRIPTION instead of the file names? Maybe by using a meta tag line?

And -- Can we exclude certain files that reside in that directory?

Last but not least -- if no search results are found, can we have it say something (with a "back" button) rather than just getting a totally blank page?

If we can accomplish this, I will send you at least 500 NP.

Thanks for all your help.

Gene
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back