search - RESOLVED

I’ve been trying to create a basic search for my site. The whole site is run on a [really simple] include based code I made. The search only entails a collection of titles. Since I do not mind adding each entry into the search file myself, the best that I could come up with is something like this:

[code]search.html:

Search for

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

search.php:

<?php $s = $_POST["search"]; $terms = explode(" ", $s); for($i = 0; $i < count($terms); $i++){ if ( $terms[$i] == "term1" ) {echo "Phrase associated with phrase 1
";} if ( $terms[$i] == "term2" ) {echo "Phrase associated with phrase 1
";} if ( $terms[$i] == "term3" ) {echo "Phrase associated with word 2
";} if ( $terms[$i] == "term4" ) {echo "Phrase associated with word 3
";} } ?>[/code]

This causes 1 problem though. If I were to search for “term1 term2” then the result would be a duplicate “Phrase associated with phrase 1”. How might I fix this, or, is there a better method of going about searching a simple list?

PS. I have come across many “search” codes but they are reliant on mysql which is something I don’t wish for it to be.

first of all i would create a clear data structure.

i would put all serchterms inside an array as well.

[php]
$s = $_POST[“search”];
$terms = explode(" ", $s);

$search= array();
$search[‘term1’]=“phrase 1”;
$search[‘term2’]=“phrase 1”;
$search[‘term3’]=“word 2”;
$search[‘term4’]=“word 3”;

foreach($serch as $serchterm => $output)
{
foreach($terms as $term)
{
if ( $term == $searchterm )
{
echo ‘Phrase associated with ‘.$output .’
’;
}
}
}
[/php]

and then i would simply turn the array around.
for not overwriting data u need a two-dimentional array.

[php]
$search= array();
$search[‘phrase 1’][]=“term1”;
$search[‘phrase 1’][]=“term2”;
$search[‘word 2’][]=“term3”;
$search[‘word 3’][]=“term4”;
[/php]

and then u may use in array and brake

[php]
foreach($serch as $output => serchterms)
{
foreach($searchterms as $searchterm)
{
if ( in_array($searchterm,$terms) )
{
echo ‘Phrase associated with ‘.$output .’
’;
break;
}
}
}
[/php]

but of cause this is just what i would do. to solve ur problem u just need to use in_array instead of an loop:
[php]
if ( in_array(“term1”,$terms) or in_array(“term2”,$terms)) {echo “Phrase associated with phrase 1
”;}
if ( in_array(“term3”,$terms) ) {echo “Phrase associated with word 2
”;}
if ( in_array(“term4”,$terms) ) {echo “Phrase associated with word 3
”;}
}
[/php]

Thanks for the fast response. Your idea is great. After looking at it for a while I understood it and have decided to use it. However, there is one more potential problem with the revised editions. After it finds a match it stops searching, correct? That means that if other terms have the same word it won’t find them. However, by removing the break, duplicates will once again be shown.

Eg.

$search['phrase 1'][]="phrase"; $search['phrase 1'][]="1"; $search['phrase 2'][]="phrase"; $search['phrase 2'][]="2";
“phrase 1”, and “phrase 2” are both titles
searching for “phrase” will reveal phrase 1 and stop so “phrase 2” wouldn’t be shown.

sorry u would have to swich the inner and th outer loop and use brake instead of brake 2 in my example.

that makes the inner loop stop and the other one continues running.

i edit my first post.

Sponsor our Newsletter | Privacy Policy | Terms of Service