Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.54 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Highlighting Keyword in search results
  2. <?php
  3. $mysql = new mysqli($host,$username,$password,$database);
  4.  
  5. $keyword = 'racing';
  6. $query = $mysql->query("SELECT * FROM products WHERE title LIKE '%$keyword%' OR description LIKE '%$keyword%'");
  7.  
  8. while($result = $query->fetch_object())
  9. {
  10.     echo '<p>';
  11.     echo $result->title.'<br />';
  12.     echo substr($result->description,'0','256');
  13.     echo '</p>';
  14. }
  15. ?>
  16.        
  17. $result->title = preg_replace('/($keyword)/i','<span class="highlight">$1</span>', $result->title));
  18.        
  19. .highlight { background-color: #ffa; }