I have a working php search program: at http://www.thebicyclesite.com/databa...ticleslist.php

The code, saved as a .txt file here: http://www.thebicyclesite.com/databa...eslist-php.txt

I also have two routines that will allow the search results for this specific code to be highlighted.

I am unable to figure out where to placed this code, and does either of these replace existing code.

I am also uncertain about what to do the the CODE tags at the top and bottom of each example., as they seem to be rejected.

Can anyone help?

Glenn A (webmaster)


Highlight option 1.



// get the query string

Code:
if ($_GET["psearch"] != "") {
$psearchquery = str_replace("?psearch=", "", str_replace($_SERVER['PHP_SELF'], "", $_SERVER['REQUEST_URI']));
} else {
$psearchquery = "" ;
}
// and then on prev, next, page links add this e.g. for prev page:

Code:
<a href="productslist.php?start=<?php echo $PrevStart; echo "&psearch=$psearchquery"; ?>"><b>Prev</b></a>

Highlight Option 2

Code:
<?php

// ***************************************************************************
// HIGHLIGHT SEARCH TERMS
// ***************************************************************************

function highlight($text, $search)
{
$colors = array('#FFFF00','#00FFFF','#99FF99','#FF9999','#FF66FF',
'#880000','#00AA00','#886800','#004699','#990099');

$terms = array();
preg_match_all('/(".+?"|\S+)/', $search, $m);
foreach (array_unique($m[0]) as $s) $terms[] = str_replace('"', '', $s);

$r = $text;
for ($i = 0; $i < count($terms); $i++) {
$blocks = preg_split('/(<.+?'.'>)/s', $r, -1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$r = '';
for ($j = 0; $j < count($blocks); $j++) {
if (substr($blocks[$j], 0, 1) != '<') {
$replace = '<b style="color:'.($i / 5 % 2 ? 'white':'black').
';background-color:'.$colors[$i % 10].'">\\1</b>';
$blocks[$j] = preg_replace('/('.preg_quote($terms[$i]).')/i',
$replace, $blocks[$j]);
}
$r .= $blocks[$j];
}
}
return $r;
}

?>
Then you can call the function just like this:
Code:
<?php echo highlight($_x_fldname, $psearch); ?>