I'm having trouble with my search script.
Basically, all is fine with the search if the result is found but if there are no matches in the DB(MySQL) then my error doesn't display.. am i missing something? heres the code:
<?php
$term = $_GET['term'];
$sql = mysql_query("select * from search_e where content like '%$term%'");
while ($row = mysql_fetch_array($sql)){
$data = $row['content'];
$first_pos = strpos($data,$term);
if ($first_pos !== false) {
$output = substr($data,max(0,$first_pos - 100),200 + strlen($term));?>
<div>
<p class="ptitle"><?php echo $row["fn"]; ?></p><hr>
Brief summary of contents:
<hr class="hr">
<p style="padding: 5px;">
<i>"<?php echo $output; ?>" </i>..
</p>
</div><br><br>
<?php
}
else {?>
<div><?php echo "Sorry! No results were found using term: ".$_GET['term']."<br>Try using fewer Keywords"; ?></div>
<?php }?>
<?php
}
//close
mysql_close();
?>
This may be something simple im doing wrong but i just cant figure it out. Also i know the code is dirty but its how i work.
I was also hoping to implement a little snippet i found browsing the net, which higlights specific words in a phrase.
function highlight($sString, $aWords) {
if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) {
return false;
}
$sWords = implode ('|', $aWords);
return preg_replace ('@\b('.$sWords.')\b@si', '<strong style="background-color:yellow">$1</strong>', $sString);
}
Is this possible to work into my script??