Jump to content


Photo

Adding boolean search to existing search script


  • Please log in to reply
1 reply to this topic

#1 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 02 July 2006 - 04:01 PM

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:

<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>


Here is my search processing / results page

<?
//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;
?>


Thanks,

John
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking

#2 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 02 July 2006 - 05:14 PM

hello,
        http://www.joedolson...n-PHP-MySQL.php this will help you.
www.karthi.us




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users