Keyword highlight help
Hi, I would like to highlight the search terms in my results using DA Search.
Is this possible?
Thanks
Hi, I would like to highlight the search terms in my results using DA Search.
Is this possible?
Thanks
it _can_ be done, but is not supported and requires some hand coding to accomplish.
one way it could be done would be to store the search terms in a session variable, then using the php preg_replace() function, search the rescordset results for the string, and replace with a span tag using the color style
the following code is an example of how this can be done, in this example the search forms method was set to POST.
the first block sets session variables for each POST element using the name "['searchTerms']['<elementName>']". so if I have a form element named "name", the value of the name form element will be stored in a session named "['searchTerms']['name']"
the second block is a function to do the highlighting, it takes 2 arguments, the name of the element and the data to search.
<?php
@session_start();
if($_SERVER['REQUEST_METHOD'] == "POST") {
if(isset($_SESSION['searchTerms'])) unset($_SESSION['searchTerms']);
foreach($_POST as $k => $v) {
if($v != "") {
$_SESSION['searchTerms'][$k] = $v;
}
}
}
function doHighLight($elemName, $haystack) {
if(isset($_SESSION['searchTerms'][$elemName]) && $_SESSION['searchTerms'][$elemName] != "") {
$termsArr = explode(" ", $_SESSION['searchTerms'][$elemName]);
$needleArr = array();
for($i=0;$i< sizeof($termsArr) ; $i++) {
$needleArr[] = "{".$termsArr[$i]."\b}i";
}
$retstr = "";
$retstr = preg_replace($needleArr, "<span style=\"color:red;\">$0</span>", $haystack);
return $retstr;
} else {
return $haystack;
}
}
?>
in the results page, the code to display a recordset column may look like this:
<?php echo $row_recordsetName['ProductName']); ?>
we can change this to perform the highlighting by adding the doHighLight function call around it.
in my example,, the name of the form element that searches the "ProductName" column is "name", so the doHighLight function call will look like this:
<?php echo doHighLight("name", $row_recordsetName['ProductName']); ?>
like i said, this is not supported functionality, but with some familiarity with PHP coding, you should be able to use the example as a starting point.
thanks a lot Jason, I will try this.
Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.
These out-of-the-box solutions provide you proven, tested applications that can be up and running now. Build a store, a gallery, or a web-based email solution.