如何使用 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;
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 中的搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!