Reputation: 19
Here is my code so far
<form method="post" name="search">
<input type="search" name="k" placeholder="Enter Keywords">
<input type="submit" value="Search">
</form>
<?
$skw = $_POST['k'];
if(isset($_POST['k'])){
$connect = mysql_connect('*', '*', '*')or die('Could not execute command. Please contact a administrator.');
mysql_select_db('*')or die('No database');
$query = "SELECT * FROM *** WHERE items LIKE '%$skw%'";
$result = mysql_query($query)or die(mysql_error());
echo "<table align=\"left\" border=\"1\" cellpadding=\"5\" cellspacing=\"3\">";
echo "\"" . $skw . "\"";
if(empty($result)){echo " Not Found!";}else{
echo " Found In";
while($row = mysql_fetch_array($result)){
echo "<tr><td>";
echo "<font color=\"#CC0000\">" . $row['tub_id'] . "</font>";
echo "</td><td>";
echo $row['items'];
echo "</td></tr>";
}
echo "</table>";
}}?>
How do I highlight the $skw in the result? I have tried using preg_replace, but i am very new to php and cannot get it to work. I understand that I can use classes to style it. Thank you for any help.
Upvotes: 1
Views: 338
Reputation: 1573
First add a css high lightlight
.highlight{background-color: yellow; }
then assuming that $skw
is the search keyword and you will search it in row['items']
in your while statement
while($row = mysql_fetch_array($result)){
echo "<tr><td>";
echo "<font color=\"#CC0000\">" . $row['tub_id'] . "</font>";
echo "</td><td>";
$items = preg_replace("/" . $skw. "/", "<span class='highlight'>'" . $skw . "</span>",$row['items']);
echo $items;
echo "</td></tr>";
}
then some notes are: don't use the <font>
tag. -_-
and mysql_*
is deprecated.. :D
Upvotes: 2
Reputation: 3470
You can add a class
css sytle:
.hihglight{background-color: yellow;}
php code:
echo "<span class='hihglight'>{$skw}</span>";
As side Note: Mysql_* extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
A useful link Why shouldn't I use mysql_* functions in PHP
Upvotes: 0