dennismonsewicz Posted March 25, 2008 Share Posted March 25, 2008 I have a full text search engine on my site. I would like to color code their search. For Example: If a user searches for "Cat", I would like to to be able to highlight the word cat with a color throughout the results. Any ideas? Link to comment https://forums.phpfreaks.com/topic/97857-color-code-search/ Share on other sites More sharing options...
Northern Flame Posted March 25, 2008 Share Posted March 25, 2008 you could probably try str_replace() to replace the search term with <font color="red">$searchterm</font> from the text being displayed Link to comment https://forums.phpfreaks.com/topic/97857-color-code-search/#findComment-500706 Share on other sites More sharing options...
dennismonsewicz Posted March 25, 2008 Author Share Posted March 25, 2008 Well the problem with that is that it won't work in the SQL statement. Link to comment https://forums.phpfreaks.com/topic/97857-color-code-search/#findComment-500709 Share on other sites More sharing options...
Northern Flame Posted March 25, 2008 Share Posted March 25, 2008 well you could add it to your while loop, like this: while($row = mysql_fetch_array($query){ $new_text = str_replace($search_term, "<font color=\"red\">$search_term</font>", $row['text']); echo nl2br($new_text); } Link to comment https://forums.phpfreaks.com/topic/97857-color-code-search/#findComment-500714 Share on other sites More sharing options...
dennismonsewicz Posted March 25, 2008 Author Share Posted March 25, 2008 that didn't work, all of the results in one of the columns disappeared Link to comment https://forums.phpfreaks.com/topic/97857-color-code-search/#findComment-500720 Share on other sites More sharing options...
dennismonsewicz Posted March 25, 2008 Author Share Posted March 25, 2008 NEVERMIND I AM AN IDIOT! TOPIC SOLVED! Link to comment https://forums.phpfreaks.com/topic/97857-color-code-search/#findComment-500721 Share on other sites More sharing options...
Barand Posted March 25, 2008 Share Posted March 25, 2008 try this if you want SQL to do the work <?php include 'db.php'; $sql = "SELECT DISTINCT REPLACE(locname, 'ham', '<span style=\'color: red; font-weight: 600\'>ham</span>') FROM towns WHERE locname LIKE '%ham'" ; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); while ($r = mysql_fetch_row($res)) { echo $r[0], '<br>'; } ?> Link to comment https://forums.phpfreaks.com/topic/97857-color-code-search/#findComment-500731 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.