在mysql PHP搜索中突出显示搜索词 [英] Highlight search term in mysql php search

查看:65
本文介绍了在mysql PHP搜索中突出显示搜索词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我在我的php搜索代码中突出显示搜索词吗? 下面是我当前正在使用的代码,它工作正常.只是想添加突出显示功能,但不知道如何在不重做整个事情的情况下在此代码上实现该功能.

Could someone please help me to highlight the searchterm in my php search code? Below is the code that I am currently using, and it works fine. Would just like to add a highlight function but have no idea how to implement that on this code without redoing the whole thing.

我遇到了在mysql php search中突出显示搜索文本看起来很好.但是我迷失了实现这一目标的方法.一段时间前,我有一个<span>效果,但无法将它放入<table>中以仅突出显示搜索词并仍然在表中循环.

I came across Highlight search text in mysql php search thispost which looks very nice. But I'm lost trying to implement this. Some time ago I had a <span> effect, but couldn't get it into the <table> to highlight only the searchterm and still loop through the 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);

推荐答案

最简单的解决方案是使用 str_replace() 将搜索字词替换为环绕在其周围的<span>标记样式的

The simplest solution is to use str_replace() to replace the search term with <span> tags wrapped around them, styled.

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

Warning: The way you have your script set up, you're vulnerable to injection attacks. This is just an example to show you how to pass in variables.

请参阅:如何防止在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&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>

这篇关于在mysql PHP搜索中突出显示搜索词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆