Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP/MySQL FULLTEXT Search, show result relevance in %

Status
Not open for further replies.

dreamaz

Technical User
Dec 18, 2002
184
CA
Hi Everyone,

I have recently added a knowledge base to my website using MYSQL fulltext indexing. I have the search and retrieval working perfectly, but would like to ahve the % relevance displayed as well. How would I go about getting this done?

Here is what i have so far:

// Create the search function:

function searchForm()
{
// Re-usable form

// variable setup for the form.
$searchwords = (isset($_GET['words']) ? htmlspecialchars(stripslashes($_REQUEST['words'])) : '');
$normal = (($_GET['mode'] == 'normal') ? ' selected="selected"' : '' );
$boolean = (($_GET['mode'] == 'boolean') ? ' selected="selected"' : '' );

echo '<form method="get" action="'.$_SERVER['PHP_SELF'].'">';
echo '<input type="hidden" name="cmd" value="search" />';
echo 'Search for: <input type="text" name="words" value="'.$searchwords.'" />&nbsp;';
echo 'Mode: ';
echo '<select name="mode">';
echo '<option value="normal"'.$normal.'>Normal</option>';
echo '<option value="boolean"'.$boolean.'>Boolean</option>';
echo '</select>&nbsp;';
echo '<input type="submit" value="Search" />';
echo '</form>';
}


// Create the navigation switch
$cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : '');

switch($cmd)
{
default:
echo '<h1>Search Database!</h1>';
searchForm();

break;


case "search":
searchForm();
// echo '<h3>Search Results:</h3><br />';

$searchstring = mysql_escape_string($_GET['words']);
switch($_GET['mode'])
{
case "normal":
$sql = "SELECT * FROM KB_TEST
WHERE MATCH (Problem, ArticleTitle, Cause, Symptom)
AGAINST ('$searchstring' IN BOOLEAN MODE)";
break;

case "boolean":
$sql = "SELECT * FROM KB_TEST
WHERE MATCH (Problem, ArticleTitle, Cause, Symptom)
AGAINST ('$searchstring' IN BOOLEAN MODE)";
break;
}

// echo $sql;

$result = mysql_query($sql) or die (mysql_error());

while($row = mysql_fetch_object($result))
{ ?>
<font size="2" face="Tahoma"><strong><a href=" echo ($row->KBID); ?>" target="_blank"><?php echo ($row->Problem); ?> </a></strong></font><br>
<font size="1" face="Tahoma">
<?php
echo ' '.stripslashes(htmlspecialchars($row->ArticleTitle));
echo '<br>'.stripslashes(htmlspecialchars($row->Cause)); ?>
<font color="#CCCCCC" size="1" face="Tahoma"><em><br>
echo ($row->KBID);
echo '<hr size="1" />'; ?></em></font>
<?php }
break;
}
?>

Any help is appreciated. Thanks

dr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top