[英]Highlight search term in mysql php search
有人可以幫我在我的PHP搜索代碼中突出顯示searchterm嗎? 下面是我當前正在使用的代碼,它工作正常。 只是想添加突出顯示功能,但不知道如何在不重做整個事情的情況下在此代碼上實現該功能。
我在mysql php search thispost中遇到了Highlight搜索文本 ,看起來非常不錯。 但是我迷失了實現這一目標的方法。 一段時間前,我有一個<span>
效果,但無法將其放入<table>
以僅突出顯示搜索詞,並且仍然循環遍歷該表。
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 ";
echo "<b><font size='15' color='blue'>$num_rows</font></b>";
echo " 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);
最簡單的解決方案是使用str_replace()
將搜索詞替換為環繞它們的<span>
標記樣式。
警告:設置腳本的方式很容易受到注入攻擊。 這只是一個示例,向您展示如何傳遞變量。
請參閱: 如何防止PHP中的SQL注入?
<?php
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);
$term = $_POST[searchterm];
$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 ";
echo "<b><font size='15' color='blue'>$num_rows</font></b>";
echo " 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>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['who']) . "</td>";
echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['ref']) . "</td>";
echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['asset']) . "</td>";
echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['make_model']) . "</td>";
echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['serial']) . "</td>";
echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['os']) . "</td>";
echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['office']) . "</td>";
echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['swp']) . "</td>";
echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['ea']) . "</td>";
echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['dt_in']) . "</td>";
echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['status']) . "</td>";
echo "<td>" . str_replace($term, "<span class=\"highlight\">$term</span>", $row['dt_out']) . "</td>";
}
echo "</table>";
echo "<br /><br />";
echo "</center>";
mysql_close($con);
?>
以及一些示例樣式:
<style type="text/css">
.highlight { background-color: yellow; }
</style>
我看不到您在頁面上打印搜索字詞的位置。 另外,例如,我會使用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
您將要使用正則表達式( preg_replace
)搜索您的術語,並將其替換為用<span>
</span>
包圍的所述術語。
請參閱preg_replace
的文檔以了解如何使用它: http : //us3.php.net/preg_replace
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.