突出显示 php/mysql 中的搜索结果

时间:2022-11-15
本文介绍了突出显示 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 个参数;

  1. 第一个是您要查找的内容
  2. 第二个是应该改成什么
  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;

  1. the first one is what you are looking for
  2. second one is what should it be changed to
  3. 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 中的搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:mysql FULLTEXT 搜索多个单词 下一篇:如何在 GridView::widget, Yii2 的搜索框中使用简单的下拉列表?

相关文章

最新文章