how do i make a search form work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

how do i make a search form work

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: how do i make a search form work

Post 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
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: how do i make a search form work

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: how do i make a search form work

Post 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.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: how do i make a search form work

Post 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?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: how do i make a search form work

Post 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 :)
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: how do i make a search form work

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: how do i make a search form work

Post 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
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: how do i make a search form work

Post 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.
Post Reply