突出显示mysql php搜索中的搜索词

时间:2014-01-22 19:32:28

标签: php mysql search highlight

有人可以帮我突破我的php搜索代码中的searchterm吗? 下面是我目前使用的代码,它工作正常。只想添加一个高亮功能,但不知道如何在不重做整个代码的情况下在这段代码上实现它。

我遇到了Highlight search text in mysql php search这个看起来非常好的帖子。但我失去了试图实现这一点。前段时间我有一个<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&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);

3 个答案:

答案 0 :(得分:7)

最简单的解决方案是使用str_replace()将搜索字词替换为围绕它们的<span>标记,并设置样式。

警告:您设置脚本的方式很容易受到注入攻击。这只是向您展示如何传入变量的示例。

请参阅:How can I prevent SQL injection in PHP?

<?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&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>" . 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>

答案 1 :(得分:0)

我看不到您在页面上打印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

答案 2 :(得分:0)

您需要使用正则表达式(preg_replace)来搜索您的字词,并将其替换为<span> </span>所包围的字词。

查看preg_replace的文档,了解如何使用它:http://us3.php.net/preg_replace

相关问题