highlighting search results in php/mysql(突出显示 php/mysql 中的搜索结果)
问题描述
如何使用 php 突出显示来自 mysql 查询的搜索结果?
这是我的[修改]代码:
$search_result = "";$search_result = $_GET["q"];$result = mysql_query('SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes WHERE cQuotes LIKE "%' . $search_result .'%" ORDER BY idQuotes DESC', $conn)或死('错误:'.mysql_error());函数 h($s) {echo htmlspecialchars($s, ENT_QUOTES);}?><div class="caption">搜索结果</div><div class="center_div"><表格><?php while ($row= mysql_fetch_array($result)) { ?><?php $cQuotes = preg_replace($search_result, "<div class='highlight'>".$search_result."</div>", $row['cQuotes']);?><tr><td style="text-align:right; font-size:15px;"><?php h($row['cArabic']) ?></td><td style="font-size:16px;"><?php h($cQuotes) ?></td><td style="font-size:12px;"><?php h($row['vAuthor']) ?></td><td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']) ?></td></tr><?php } ?>
您可以使用 preg_replace();,当它在您的文本中找到匹配项时,您可以在匹配的单词周围放置一个带有突出显示类的 div.然后,您将向高亮类添加背景颜色和边框以使其脱颖而出
preg_replace 需要 3 个参数;
- 第一个是您要查找的内容
- 第二个是应该改成什么
- 他应该搜索和替换的文本字符串
例如
<表格><caption>搜索结果</caption><?php while ($row= mysql_fetch_array($result)) { ?><?php $arabic = preg_replace("/".$search_result."/", "<div class='highlight'>".$search_result."</div>", h($row['阿拉伯语']));?><tr><td style="text-align:right; font-size:15px;"><?php $arabic ?></td><td style="font-size:16px;"><?php h($row['cQuotes']) ?></td><td style="font-size:12px;"><?php h($row['vAuthor']) ?></td><td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']) ?></td></tr><?php } ?>
我只为阿拉伯语做了这件事,但您可能也需要为 cQuotes、vAuthor 和 vReference 这样做.
how do i highlight search results from mysql query using php?
this is my [modified] code:
$search_result = "";
$search_result = $_GET["q"];
$result = mysql_query('SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes WHERE cQuotes LIKE "%' . $search_result .'%" ORDER BY idQuotes DESC', $conn)
or die ('Error: '.mysql_error());
function h($s) {
echo htmlspecialchars($s, ENT_QUOTES);
}
?>
<div class="caption">Search Results</div>
<div class="center_div">
<table>
<?php while ($row= mysql_fetch_array($result)) { ?>
<?php $cQuotes = preg_replace($search_result, "<div class='highlight'>".$search_result."</div>", $row['cQuotes']); ?>
<tr>
<td style="text-align:right; font-size:15px;"><?php h($row['cArabic']) ?></td>
<td style="font-size:16px;"><?php h($cQuotes) ?></td>
<td style="font-size:12px;"><?php h($row['vAuthor']) ?></td>
<td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']) ?></td>
</tr>
<?php } ?>
</table>
</div>
you could use preg_replace();, when it finds a match in your text you could place a div with a class of highlight around the matching word. You would then add a background color and border to the highlight class to make it stand out
preg_replace expect 3 parameters;
- the first one is what you are looking for
- second one is what should it be changed to
- The string of text he should search and replace from
so for example
<div class="center_div">
<table>
<caption>Search Results</caption>
<?php while ($row= mysql_fetch_array($result)) { ?>
<?php $arabic = preg_replace("/".$search_result."/", "<div class='highlight'>".$search_result."</div>", h($row['cArabic'])); ?>
<tr>
<td style="text-align:right; font-size:15px;"><?php $arabic ?></td>
<td style="font-size:16px;"><?php h($row['cQuotes']) ?></td>
<td style="font-size:12px;"><?php h($row['vAuthor']) ?></td>
<td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']) ?></td>
</tr>
<?php } ?>
</table>
</div>
I only did it for arabic but you might need to do that for cQuotes, vAuthor and vReference as well.
这篇关于突出显示 php/mysql 中的搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:突出显示 php/mysql 中的搜索结果
基础教程推荐
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01