突出搜索詞在MySQL PHP搜索



Answers

我沒有看到你在哪裡打印你的頁面上的searchterm。 另外,我會使用CSS樣式表,避免字體標籤,例如

 <style>

 .searchTerm{
    background-color:red;
    } 
 </style>


 <table>
   <tr><th>You searched for<div class='searchTerm'><?php echo $_POST[searchterm];?></div></th></tr>
  //rest of page
Question

有人可以幫我突出我的PHP搜索代碼中的searchterm? 下面是我目前使用的代碼,它工作正常。 只是想添加一個高亮功能,但不知道如何實現這個代碼沒有重做整個事情。

我碰到突出搜索文本在MySQL的PHP​​搜索 thispost這看起來很不錯。 但我失去了試圖實現這一點。 前一段時間我有一個<span>效果,但是無法將它放到<table> ,只突出顯示searchterm,並在表格中循環。

include("config/config.php");
$con = mysql_connect($host, $db_user, $db_pass);
if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

mysql_select_db($db, $con);

$result = mysql_query("SELECT * FROM data WHERE `data_id` LIKE '%$_POST[searchterm]%'
OR `who` LIKE '%$_POST[searchterm]%'
OR `ref` LIKE '%$_POST[searchterm]%'
OR `asset` LIKE '%$_POST[searchterm]%'
OR `make_model` LIKE '%$_POST[searchterm]%'
OR `serial` LIKE '%$_POST[searchterm]%'
OR `os` LIKE '%$_POST[searchterm]%'
OR `swp` LIKE '%$_POST[searchterm]%'
OR `ea` LIKE '%$_POST[searchterm]%'
OR `dt_in` LIKE '%$_POST[searchterm]%'
OR `status` LIKE '%$_POST[searchterm]%'
OR `dt_out` LIKE '%$_POST[searchterm]%'
");
$num_rows = mysql_num_rows($result);

echo "<center>";
echo "<BR><BR>";
echo "<a href='index.php'><button id='sblogloginbtn' name='login' type='submit'><b>BACK</b></button></a>";
echo "<BR><BR>";
echo "<h1>Your search has found&nbsp;";
echo "<b><font size='15' color='blue'>$num_rows</font></b>";
echo "&nbsp;records.</font></h1>";
echo "<BR><BR>";

echo "<table border='frame'>
<tr style='color:#FF00FF'>
<th>Signed in By</th>
<th>Reference Number</th>
<th>Asset Number</th>
<th>Make Model</th>
<th>Serial Number</th>
<th>Operating System</th>
<th>Office</th>
<th>Profile</th>
<th>Extra Apps</th>
<th>Time IN</th>
<th>Status</th>
<th>Time OUT</th>
</tr>";

while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['who'] . "</td>";
    echo "<td>" . $row['ref'] . "</td>";
    echo "<td>" . $row['asset'] . "</td>";
    echo "<td>" . $row['make_model'] . "</td>";
    echo "<td>" . $row['serial'] . "</td>";
    echo "<td>" . $row['os'] . "</td>";
    echo "<td>" . $row['office'] . "</td>";
    echo "<td>" . $row['swp'] . "</td>";
    echo "<td>" . $row['ea'] . "</td>";
    echo "<td>" . $row['dt_in'] . "</td>";
    echo "<td>" . $row['status'] . "</td>";
    echo "<td>" . $row['dt_out'] . "</td>";
    }
echo "</table>";
echo "<br /><br />";
echo "</center>";

mysql_close($con);



Links