Jump to content

Archived

This topic is now archived and is closed to further replies.

john-formby

Adding boolean search to existing search script

Recommended Posts

Hi,

I have a search script running on my website but it does not support boolean operators.  I am finding that the results I am getting are very limited and many are being ignored as the words are not next to each other in the keyword row.

I have tried to follow a couple of tutorials I found on the net but have been unsuccessful in implementing them.  What I want is to be able to search through the keywords row in my database using boolean operators and display the results as my current search does, but in order of relevance.

I would really appreciate some help with this as I am not having any success.

Here is my search form:

[code]<form name="search" method="post" action="srch.php">
<input class="form3" type="text" name="find" maxlength="255" />
<input style="vertical-align: top;" class="submit" type="submit" name="search" value="Go" />
<input type="hidden" name="searching" value="yes" />
</form>[/code]


Here is my search processing / results page

[code]<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// Otherwise we connect to our Database
mysql_connect("localhost", "USER", "PASS") or die(mysql_error());
mysql_select_db("pagelinks") or die(mysql_error());

// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM links WHERE keywords LIKE '%$find%'");

$num_per_page=10;

$num=mysql_num_rows($data);

$num_pages=ceil($num/$num_per_page);

$pages=Array();

for ($i=0;$i<$num_pages;$i++)
$pages[]="<a href=\"?searching=yes&find=$find&start=".$i."\">[".($i+1)."] </a>";

$pages=implode("&nbsp;",$pages); #Here's your string with the page links, output where ever

if ($start<0)
$start=0;

$x=($start*$num_per_page);
$y=$num_per_page;

$query="SELECT * FROM links WHERE keywords LIKE '%{$find}%' LIMIT {$x},{$y}";
$data = mysql_query($query);

//And we display the results
while($result = mysql_fetch_array( $data ))
{

echo "<hr /><br />";
if ($result['new_window'] == 1) {
echo "<b><a href=\"".$result['url']."\" target=\"_new\">".$result['link_text']."</a></b>";
} else {
echo "<b><a href=\"".$result['url']."\">".$result['link_text']."</a></b>";
}
echo "<br />";
echo $result['description'];
echo "<br />";
if ($result['new_window'] == 1) {
echo "<a href=\"".$result['url']."\" target=\"_new\">".$result['url']."</a>";
} else {
echo "<a href=\"".$result['url']."\">".$result['url']."</a>";
}
echo "<br /><br />";
}

echo "<hr /><br />";

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}

echo "<br /><br />";
echo $pages;
?>[/code]


Thanks,

John

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.