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? Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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); } Quote Link to comment 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 Quote Link to comment 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! Quote Link to comment 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.