Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Highlighting search terms in a results page?

0 views
Skip to first unread message

Iain71

unread,
Aug 26, 2006, 12:45:44 PM8/26/06
to
I've just been trying out webassist's database search to put together some
search and results pages.

It doesn't however highlight search terms in the results page, which is
something I need to be able to do.

My results page currently looks like :

<?php require_once('../Connections/connSearchTechUK.php'); ?>
<?php
//WA Database Search Include
require_once("../WADbSearch/HelperPHP.php");
?>
<?php
mysql_select_db($database_connSearchTechUK, $connSearchTechUK);
$query_Recordset1 = "SELECT * FROM Candidates";
$Recordset1 = mysql_query($query_Recordset1, $connSearchTechUK) or
die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$TFM_LimitLinksEndCount = 5;
$TFM_temp = $pageNum_Vacancies + 1;
$TFM_startLink = max(1,$TFM_temp - intval($TFM_LimitLinksEndCount/2));
$TFM_temp = $TFM_startLink + $TFM_LimitLinksEndCount - 1;
$TFM_endLink = min($TFM_temp, $totalPages_Vacancies + 1);
if($TFM_endLink != $TFM_temp) $TFM_startLink = max(1,$TFM_endLink -
$TFM_LimitLinksEndCount + 1);

//WA Database Search (Copyright 2005, WebAssist.com)
//Recordset: Recordset1;
//Searchpage: newsearch.php;
//Form: form1;
$WADbSearch1_DefaultWhere = "";
if (!session_id()) session_start();
if ((isset($_POST["WADbSearch1"])) && ($_POST["WADbSearch1"] != "")) {
$WADbSearch1 = new FilterDef;
$WADbSearch1->initializeQueryBuilder("MYSQL","1");
//keyword array declarations
$KeyArr0 = array("CV");

//comparison list additions

$WADbSearch1->keywordComparison($KeyArr0,"".((isset($_POST["keyword"]))?$_POST["
keyword"]:"") ."","AND","Includes",",%20","%20","%22","%22",0);

//save the query in a session variable
if (1 == 1) {
$_SESSION["WADbSearch1_newresults"]=$WADbSearch1->whereClause;
}
}
else {
$WADbSearch1 = new FilterDef;
$WADbSearch1->initializeQueryBuilder("MYSQL","1");
//get the filter definition from a session variable
if (1 == 1) {
if (isset($_SESSION["WADbSearch1_newresults"]) &&
$_SESSION["WADbSearch1_newresults"] != "") {
$WADbSearch1->whereClause = $_SESSION["WADbSearch1_newresults"];
}
else {
$WADbSearch1->whereClause = $WADbSearch1_DefaultWhere;
}
}
else {
$WADbSearch1->whereClause = $WADbSearch1_DefaultWhere;
}
}
$WADbSearch1->whereClause = str_replace("\\''", "''",
$WADbSearch1->whereClause);
$WADbSearch1whereClause = '';
?>
<?php
mysql_select_db($database_connSearchTechUK, $connSearchTechUK);
$query_Recordset1 = "SELECT * FROM Candidates";
setQueryBuilderSource($query_Recordset1,$WADbSearch1,false);
$Recordset1 = mysql_query($query_Recordset1, $connSearchTechUK) or
die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_connSearchTechUK, $connSearchTechUK);
$query_Recordset1 = "SELECT * FROM Candidates";
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SearchTechUK - technical recruitment consultancy specialising in the
aviation aerospace industry - current vacancies</title>


<link href="../formstyle.css" rel="stylesheet" type="text/css">
<link href="../candidates.css" rel="stylesheet" type="text/css">
<link href="../pagelinks.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style17 {font-size: 12px}
.style19 {font-size: 12px; font-weight: bold; }

.editdetails {
position: relative;
background-image: url(../Images/edit8.gif);
background-repeat: no-repeat;
white-space: nowrap;
display: block;
width: 27px;
height: 18px;
margin: 0;
padding: 0;
}

.editdetails img {width: 27px; height: 18px; border: 0; }
* html a:hover {visibility:visible}
.editdetails a:hover img{visibility:hidden}

.editprofile {
position: relative;
background-image: url(../Images/profile6.gif);
background-repeat: no-repeat;
white-space: nowrap;
display: block;
width: 27px;
height: 18px;
margin: 0;
padding: 0;
}

.editprofile img {width: 27px; height: 18px; border: 0; }
* html a:hover {visibility:visible}
.editprofile a:hover img{visibility:hidden}

.delete {
position: relative;
background-image: url(../Images/delete8.gif);
background-repeat: no-repeat;
white-space: nowrap;
display: block;
width: 27px;
height: 18px;
margin: 0;
padding: 0;
}

.delete img {width: 27px; height: 18px; border: 0; }
* html a:hover {visibility:visible}
.delete a:hover img{visibility:hidden}
-->
</style>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length;
i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)
x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document;
if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++)
x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array;
for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc)
x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
</head>

Iain71

unread,
Aug 26, 2006, 12:46:18 PM8/26/06
to
[cont]

<body
onLoad="MM_preloadImages('../Images/edit4.gif','../Images/profile2.gif','../Imag
es/delete4.gif')">
<div id="wrapper">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><img src="../Images/bannerTop.png" alt="Aviation vacancies"></td>
</tr>
<tr>
<td><div id="navcontainer">
<ul id="navlist">
<li id="active"><a href="../homepage.htm" id="current">Home</a></li>
[LI]<a href="../client.htm">Client Services</a></li>
[LI]<a href="../candidate.htm">Candidate Services</a></li>
[LI]<a href="../vacancies.htm">Vacancies</a></li>
[LI]<a href="../psychometric.htm">Psychometric Services</a></li>
[LI]<a href="../contact.htm">Contact Us</a></li>
[/BULLET]
</div></td>
</tr>
<tr>
<td><img src="../Images/bannerBottom.png" alt="Technical aviation vacancies"
border="0"></td>
</tr>
</table>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"><img src="../Images/cansearchresults.gif" width="219"
height="21" vspace="10"></td>
</tr>

<td width="77%" valign="top">

<div id="candidatelinks">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="6%" class="labelcell">ID</td>
<td width="38%" class="labelcell">Candidate</td>
<td width="38%" class="labelcell">Telephone</td>
<td colspan="3" class="labelcell">Actions</td>
</tr>

<?php do { ?>
<tr>
<td class="idcell"><?php echo $row_Recordset1['CandidateID']; ?></td>
<td class="fieldcell"><a href="candidateCVDetails.php?CandidateID=<?php echo
$row_Recordset1['CandidateID']; ?>&amp;search=<?php echo ($_GET['keyword']);
?>"><?php echo $row_Recordset1['FirstName']; ?>&nbsp;<?php echo
$row_Recordset1['LastName']; ?></a></td>
<td class="fieldcell"><?php echo $row_Recordset1['Telephone']; ?></td>
<td width="6%" class="idcell"><div class="editdetails"><a
href="editCandidate.php?CandidateID=<?php echo $row_Recordset1['CandidateID'];
?>"><img src="../Images/edit7.gif" border="0" width="28px" height="18px"
/></a></div></td>
<td width="6%" class="idcell"><div class="editprofile"><a
href="editCanProfiles.php?CandidateID=<?php echo
$row_Recordset1['CandidateID']; ?>"><img src="../Images/profile5.gif"
border="0" /></a></div></td>
<td width="6%" class="idcell"><div class="delete"><a
href="confirmCanDeletion.php?CandidateID=<?php echo
$row_Recordset1['CandidateID']; ?>"><img src="../Images/delete7.gif" border="0"
/></a></div></td>
</tr>
<tr>
<td colspan="6" class="fieldcell"><?php echo $row_Recordset1['CV']; ?></td>
</tr>

</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<?php
if ($totalRows_Recordset1==0){
echo "<tr><td colspan='5'>*No Candidates Found*</td></tr>";
}
?>
</table>

</div>
<div id="pagelinks">
<p><span class="style19">Pages :</span>
<span class="style17">
<?php
for ($i = $TFM_startLink; $i <= $TFM_endLink; $i++) {
$TFM_LimitPageEndCount = $i -1;
if($TFM_LimitPageEndCount != $pageNum_Recordset1) {
printf('<a href="'."%s?pageNum_Recordset1=%d%s", $currentPage,
$TFM_LimitPageEndCount, $queryString_Recordset1.'">');
echo "$i</a>";
}else{
echo "[B]$i[/B]";
}
if($i != $TFM_endLink) echo("-");}
?>
</div>
</td>

</tr>
</table>

<div id="footer">
<p align="center">? SearchTechUK 2006 | <a href="../homepage.htm">home</a> |
<a href="../client.htm">client services</a> | <a
href="../candidate.htm">candidate services</a> | <a
href="../vacancies.htm">vacancies</a> | <a
href="../psychometric.htm">psychometric services</a> | <a
href="../contact.htm">contact us</a></p>
</div>

</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

-----

I have been having a look for code snippets on the net / forums etc, and found
this posted by the always helpful David Powers :

ie adding in the following script at the top of the page :

function highlightResult($search,$result) {
if (get_magic_quotes_gpc()) {
$replace = stripslashes($search);
}
else {
$replace = $search;
}
$highlighted = preg_replace("/{$search}/i","<span
class='hilite'>{$replace}</span>",$result);
return $highlighted;
}

And changing

<?php echo $row_Recordset1['CV']; ?>

to

<?php echo highlightResult($_GET['search'],$row_Recordset1['CV']); ?>

But with no joy - any ideas why this doesn't seem to work? I'm still new to
PHP, but can kind of follow what it should be doing. I presume it's something
to do with some variable names being given new names / definitions in the WA
code at the top, but not sure where to begin looking at all that.

Cheers,
Iain

Iain71

unread,
Aug 26, 2006, 1:59:33 PM8/26/06
to
I'm adapting another bit of code with the function.

The search field is called 'keyword' so I changed the instances of 'search' in
the function code to 'keyword', but I assume there's something going on in
webassist code?

Iain71

unread,
Aug 27, 2006, 2:39:49 PM8/27/06
to
Another thing I've tried doing is trying to use Tom Muck's Accented keywords
extension, and it's almost working, but not quite.

If I enter a single search word, then it highlights correctly, and is passed
through to the detail page, and highlights correctly there too.

If I enter any number of words that appear, then it only highlights where the
exact phrase is matched, even tho' the search returns results where any of the
words are found.

And if I enter a search term in inverted commas so as to restrict the search
to the exact phrase, then it doesn't highlight.

The PHP code for this looks like :

HTML Code:


<?php require_once('../Connections/connSearchTechUK.php'); ?>
<?php

function tfm_regexpReplace($originalString, $searchword, $before, $after) {
// replace a search word in a string, regardless of case
$tfm_regexp = "/(" . $searchword . ")/i";
$originalString =
preg_replace($tfm_regexp,"$before$1$after",$originalString);
return $originalString;


}
?>
<?php
//WA Database Search Include
require_once("../WADbSearch/HelperPHP.php");
?>
<?php

function highlightResult($keyword,$result) {
if (get_magic_quotes_gpc()) {
$replace = stripslashes($keyword);
}
else {
$replace = $keyword;
}
$highlighted = preg_replace("/{$keyword}/i","<span

class='hilite'>{$replace}</span>",$result);
return $highlighted;
}

mysql_select_db($database_connSearchTechUK, $connSearchTechUK);


And the highlighing code for the field looks like :

HTML Code:
<?php
$tfm_DescField = nl2br($row_Recordset1['CV']);
if (isset($HTTP_POST_VARS["keyword"]) || isset($HTTP_GET_VARS["keyword"])){
$tfm_searchField =
(isset($HTTP_POST_VARS["keyword"]))?$HTTP_POST_VARS["keyword"]:$HTTP_GET_VARS["k
eyword"];
if(!isset($splitField)) $splitField = explode(",",$tfm_searchField);
for ($i = 0; $i < sizeof($splitField); $i++) {
$tfm_DescField = tfm_regexpReplace($tfm_DescField,$splitField[$i],
"[B]<font color=#FF0000>","</font>[/B]");
}
}
echo($tfm_DescField);
?>


It must be almost there, but like most things of this nature, pretty useless
until it actually does work as advertised.

0 new messages