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.'" /> ';
echo 'Mode: ';
echo '<select name="mode">';
echo '<option value="normal"'.$normal.'>Normal</option>';
echo '<option value="boolean"'.$boolean.'>Boolean</option>';
echo '</select> ';
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
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.'" /> ';
echo 'Mode: ';
echo '<select name="mode">';
echo '<option value="normal"'.$normal.'>Normal</option>';
echo '<option value="boolean"'.$boolean.'>Boolean</option>';
echo '</select> ';
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