I am creating a search application and need some help with highlighting search terms (keywords) in retrieved document text. The application has three sections: 1) Search Form for search criteria input, 2) Search Results summary list with links, and 3) Full Document display in html. I need for the “search” term keywords from step 1 to be highlighted in the step 3 Full Document.
Here is the application code:
[php]
<?php ini_set('display_errors',1); error_reporting(E_ALL); $doctype = isset($_GET['doctype']) ? $_GET['doctype'] : ''; $search = isset($_GET['search']) ? $_GET['search'] : ''; if($search){ mysql_connect("xx", "xx", "xx") or die(mysql_error()); mysql_select_db("xx") or die(mysql_error()); $result = mysql_query( "SELECT FullDocumentID, CONCAT(ReadingNo, '-', SequenceNo) AS full_doc_number FROM Full_Documents WHERE SectionText LIKE '%$search%' AND SectionTypeID = '$doctype' Order By FullDocumentID"); $num_records = mysql_num_rows($result); // this code expects the result resource from the query to be in $result if(mysql_num_rows($result) > 0){ $links = array(); while ($row = mysql_fetch_assoc($result)){ $links[] = $row; } } } $doc = mysql_query( "SELECT SectionText FROM Full_Documents WHERE FullDocumentID = " . $_GET['FullDocumentID']); ?> .highlight_word{ background-color: yellow; }Search Form
<form method="get" action="" id="search_criteria">
Search Term <input type="text" id="" name="search"></br>
Document Type <select name="doctype"></br>
<option value="1">Text</option>
<option value="3">Background</option>
<option value="2">Reports</option>
<option value="4">Index</option>
</select></br>
<input type="submit" name="submit" value="Submit">
</form>
<h3>Search Results</h3>
<p>for: <span style="font-size: 14px; font-weight: bold; color: red;"><? echo $search; ?></span> </p>
<p>documents found: <span style="font-size: 14px; font-weight: bold; color: red;"><? echo $num_records; ?></span></p>
<?php
if($search){
if(isset($links)){
// output document links
echo 'Select a document to view:'; foreach($links as $link){ $_GET['FullDocumentID'] = $link['FullDocumentID']; // add document id to (any) existing get parameters $qs = http_build_query($_GET, '', '&'); // build the current url query string echo "{$link['full_doc_number']}
"; } } else { echo 'No matching documents found.'; } } ?>
<h3>Document Text</h3>
//DOCUMENT DATA DISPLAY BEGINS HERE//
echo "<table border='1' width='800px'>
<tr class=\"style3\">
";
while($row = mysql_fetch_array($doc))
{
echo "<tr class=\"style2\">";
echo "<td style=\"width: 700px; padding: 10px;\"><p>" .
preg_replace("/\r\n|\r|\n/", "</p><p>", $row['SectionText']) .
"</p></td>";
echo "</tr>";
}
echo "</table>";
?>
</div> <!--END OF DOCUMENT TEXT DIV-->
</body>
[/php]
I have been looking at several highlight functions from sites on the web, and it seemed this one is relatively simple, but I don’t know if it’s workable for my need.
[php]
function highlightWords($text, $words)
{
/*** loop of the array of words /
foreach ($words as $word)
{
/ quote the text for regex /
$word = preg_quote($word);
/ highlight the words /
$text = preg_replace("/\b($word)\b/i", ‘\1’, $text);
}
/ return the text ***/
return $text;
}
[/php]
I have tried various way of integrating it with the application, but no luck. Do I understand correctly that $words are keywords from the search form and $text is the full document output in step 3 of my application?
More broadly, is this function a workable solution? Or is there something better suited to this application? Any help with this would be greatly appreciated. - Dave