Hi all,
I'm currently putting together a search engine for my website, and so far, everything is going pretty smoothly.
The only thing left that I would like to do is when the search results are displayed, I want to be able to highlight the search term entered in the page that displays the results of the query.
For example, if I search for the term "dog grooming", here is a possible result, with the term "dog grooming" bolded:
"Acme Dog Grooming offers the finest and most affordable dog grooming service in your area. Contact us at (555)123-4567 for more details".
Below is a class that I am using, and would like to know how I can modify it so that I can highlight the search term entered. In the code below, the variable $_POST['searchbox']
is the search term being entered and what I'd like to hightlight.
class SearchAboutMe extends Search{
public function __construct()
{
$db = new PDO(### Database connection details ###);
$q = $db->prepare(" SELECT something FROM somewhere WHERE somecolumn LIKE ? ");
$q->execute(array('%'.$_POST['searchbox'].'%')); ### Here the query is being executed
$q->setFetchMode(PDO::FETCH_ASSOC);
$r = $q->fetch();
$this->username = $r['username'];
$this->city = $r['city'];
$this->state = ucwords(strtolower($r['state']));
$this->zipcode = $r['zip'];
$this->info = $r['info'];
### Here is where the search results get displayed
$count = $q->rowCount();
if($count >= 1){
### Results were found for search term, and the search term is contained somewhere inside
### the $this->info property
echo "<div class=results>
<a href=". $this->username .">" . $this->info . "</a></div>";
}else{
$count == 0;
echo "<div class=\"alert alert-error\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button>No results found for: <strong> " . $_POST['searchbox'] . "</strong></div>";
}
}
}
So in the example code above, the property $this->info will contain the search term ($_POST['searchbox']). How can I extract the search term from it?
Thanks very much for any help!