Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-13-2007, 06:32 PM   PM User | #1
glenna1984
New to the CF scene

 
Join Date: Jun 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
glenna1984 is an unknown quantity at this point
Highlight Search Results

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); ?>
glenna1984 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:52 PM.


Advertisement
Log in to turn off these ads.