Jump to content

[SOLVED] Highlight keyword in search results


damian0612

Recommended Posts

Hi

 

I would like to highlight the keyword in the search result on my page but am not sure how do achieve this.  I have been playing with my code for a while without success.  Here is the code:

 

<?php
$mytime = time() + 3600;  //convert to BST
$mytimestamp = date('Y-m-d H:i:s', $mytime);

$selection = $_GET["day_selected"];
if($_GET["search_all"]!="")
{$search = $_GET["search_all"];

 $result = mysql_query("
SELECT ItemAddedTime,ItemTitle,ItemLink,ItemSourceURL,ItemDescription
FROM feedItems
WHERE DATEDIFF('$mytimestamp', `ItemAddedTime`) <= 13
AND ItemTitle LIKE '%$search%'
ORDER BY ItemAddedTime DESC", $connection);  

if(!$result){ die("Database selection failed: " . mysql_error()); } 

$num_rows = mysql_num_rows($result); 
If($num_rows > 0){
echo "<table width='100%'><tr><td><b>Search results</b></td></tr>";
echo "<table width='100%'><tr><td><img src='./images/greyline.jpg' width='100%' height='1'></td></tr>";
while($row = mysql_fetch_array($result)){
$linksource=findLinkSource( $row[3] );
$mydate = date('H:i',strtotime($row[0]));
echo "<tr><td>$mydate <A href=$row[2] target='blank'>$row[1]</A> $linksource</td></tr>";} 
echo"</table><br />";
} 
exit();}

?>
[/code

So basically I want to highlight any word that matches $_GET["search_all"] in the results.  Any help would be appreciated.

Thanks
Damian

   $mydate = date('H:i',strtotime($row[0]));
   $linksource = str_replace($search, "<span>{$search}</span>", $linksource);
   echo "<tr><td>$mydate <A href=$row[2] target='blank'>$row[1]</A> $linksource</td></tr>";} 

 

Should take care of it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.