Page 1 of 1

how do i make a search form work

Posted: Wed Feb 02, 2011 3:04 pm
by danwguy
I have a site and I want to add a search feature, I can do the form but how do I make the search.php? I'm sorry I don't have any code to go off of. I am pretty decent with php and storing/using variables and whatnot but no idea how to make my site searchable i guess is the term I'm looking for here. my form the method is set to get and the action is set to search.php, just need to know how to make search.php now. Sorry for the vagueness and I am not asking to be spoonfed code, just no idea where to even start. Thank you in advance for your help.

Re: how do i make a search form work

Posted: Wed Feb 02, 2011 3:23 pm
by Jade
Gah well this depends a lot on how you want to do it. Are you searching for keywords in your file meta descriptions or do you have a database of keywords?

Since you're a noob you may want to consider this instead where all you do is copy/paste the code on your website and it does all the leg work for you: http://www.google.com/cse

Re: how do i make a search form work

Posted: Wed Feb 02, 2011 4:10 pm
by danwguy
Essentially I will have a folder full of files (templates of web designs) and I want people to be able to search for a specific file name or put in a search term and it finds files with like names.

Re: how do i make a search form work

Posted: Wed Feb 02, 2011 4:14 pm
by Jade
Okay then you're going to need to use this http://us2.php.net/manual/en/function.scandir.php to get a list of all your files and search through them for the partial name matches.

Re: how do i make a search form work

Posted: Wed Feb 02, 2011 4:58 pm
by danwguy
So then how would I impliment that into a search function? something like...

Code: Select all

$search = $_GET['searchterm'];
$dir = "/templates";
$dh  = opendir($dir);
while (false !== ($search = readdir($dh))) {
    $files[] = $filename;
}

sort($files);

print_r($files);

rsort($files);

print_r($files);

?> 
or am I way off in left field somewhere talking to the wall looking like someone who wore a helmet to school?

Re: how do i make a search form work

Posted: Thu Feb 03, 2011 10:18 am
by Jade
That's correct. You'd create an array with all of the file names then compare it to the search term. If there is a match (or partial match) then you can add that to an array of search results. You would return the final search results to the user. No helmet required :)

Re: how do i make a search form work

Posted: Fri Feb 04, 2011 2:23 pm
by danwguy
Wouldn't the name be something like this...

Code: Select all

$search = $_GET['searchterm'];
$dir = "/templates";
$dh  = opendir($dir);
while (false !== ($search = readdir($dh))) {
    $files[] = $search['filename'];
}

sort($files);

print_r($files);

rsort($files);

print_r($files);

?> 
 
Somehow this is just not making sense to me. I get the idea, I guess I'm just not grasping the full concept on how you get the file name that is like the search term and echo it back. I can understand the openening of the dir, I guess I am just confused on matching the file names in the directory to the $search variable and then echo-ing them back to the page. I have a page already that opens a folder and lists all files in that folder as clickable links to download them (they are pdf's). Like I said I am just a little slow on how to match file name to $search variable and then echo results.

Re: how do i make a search form work

Posted: Fri Feb 04, 2011 3:27 pm
by Jade
There are a few ways, like regular expressions, but you'll probably want to use this: http://php.net/manual/en/function.strpos.php

Re: how do i make a search form work

Posted: Fri Feb 04, 2011 5:30 pm
by danwguy
Ok so I think I got it, does this look right??

Code: Select all

<?php
$search = $_GET['searchterm'];
$dir = "/templates/";
if (is_dir($dir)) {
	if ($dh = opendir($dir)) {
		while (($file = readdir($dh)) !== false) {
			$results[] = strpos($file, $search);
				echo "<a href='" . $dir . $results[] ."'><img src='templates/images/' .$results[]. ' /></a><br />";
			}
			closedir($dh);
		}
	}
?>
Thank you for all your help, it's really appreciated.