WordPress的MySQL查询结果

编程入门 行业动态 更新时间:2024-10-09 16:23:16
WordPress的MySQL查询结果 - 如何突出显示表中找到的字符串(Wordpress MySQL Query Results - How to Highlight Strings Found in Displayed Table)

我创建了一个有效的WordPress页面,其中包含两个搜索框(以及它们之间的一对和/或按钮),用于查询SQL数据库并返回一个包含一个(或两者都包含)的行的两列表格,搜索字符串($ search和$ search2),然后在结果超过100行时进行分页。 我尝试了三种突出显示表中这些字符串的不同方法,但无法使其正常工作。 所以我非常感谢一些帮助。 php代码的相关部分(为此我使用WordPress插入PHP插件)是:

[insert_php] * * * * if ($andor == "and"){ $result = mysql_query("SELECT DATE(start_date), EVENT FROM {$table} WHERE EVENT REGEXP '$search' >0 AND EVENT REGEXP '$search2' >0 LIMIT {$limits}"); } else{ $result = mysql_query("SELECT DATE(start_date), EVENT FROM {$table} WHERE EVENT REGEXP '$search' >0 OR EVENT REGEXP '$search2' >0 LIMIT {$limits}"); } * * * * while($row = mysql_fetch_row($result)) { echo "<tr>"; foreach($row as $cell) echo "<td>$cell</td>"; echo "</tr>\n"; } [/insert_php]

首先,我在functions.php文件中添加了一个函数,并尝试调用它并且无法使其工作

function highlight_word( $content, $word) { $replace = '<span style="background-color: #FF0;">' . $word . '</span>'; $content = str_replace( $word, $replace, $content ); return $content; };

然后我尝试在单独的while循环中更换字符串。 最后我试着改变现有的while循环,如下所示:

while($row = mysql_fetch_row($result)) { echo "<tr>"; foreach($row as $cell) echo "<td>" . str_replace($search, '<span style="background-color: #FF0;">' . $search . '</span>', $cell) . "</td>"; echo "</tr>\n"; }

如果我可以将突出显示限制在第二列(“事件”),这将会很有帮助,但这并不比简单地将它起作用重要。 而且,是的,我将在完成之前将代码升级到mysqli并转义查询字符串。 一如既往,感谢这个伟大的社区。

I have created a functioning WordPress page with two search boxes (and a pair of and/or buttons between them) that queries an sql database and returns a two-column table with rows that contain one (or both, as the case may be) of the searched-for strings ($search and $search2) and then paginates when results exceed 100 rows. I have tried three different methods of highlighting these strings in the table but can't get it to work. So I'd much appreciate some help. The relevant portion of the php code (for which I am using the WordPress Insert PHP plug-in) is:

[insert_php] * * * * if ($andor == "and"){ $result = mysql_query("SELECT DATE(start_date), EVENT FROM {$table} WHERE EVENT REGEXP '$search' >0 AND EVENT REGEXP '$search2' >0 LIMIT {$limits}"); } else{ $result = mysql_query("SELECT DATE(start_date), EVENT FROM {$table} WHERE EVENT REGEXP '$search' >0 OR EVENT REGEXP '$search2' >0 LIMIT {$limits}"); } * * * * while($row = mysql_fetch_row($result)) { echo "<tr>"; foreach($row as $cell) echo "<td>$cell</td>"; echo "</tr>\n"; } [/insert_php]

First, I added a function to the functions.php file and tried calling it and couldn't make it work

function highlight_word( $content, $word) { $replace = '<span style="background-color: #FF0;">' . $word . '</span>'; $content = str_replace( $word, $replace, $content ); return $content; };

Then I tried a string replacement in a separate while loop. Finally I tried changing the existing while loop as follows:

while($row = mysql_fetch_row($result)) { echo "<tr>"; foreach($row as $cell) echo "<td>" . str_replace($search, '<span style="background-color: #FF0;">' . $search . '</span>', $cell) . "</td>"; echo "</tr>\n"; }

It would be helpful if I could limit the highlighting to the second column("EVENTS") only, but this is much less important than simply getting it to work. And, yes, I will upgrade code to mysqli and escape query strings before finalizing. As always, thanks to all in this great community.

最满意答案

你为什么不使用一些高亮的js库?

mark.js支持突出显示的表格

$("table tr td:nth-child(1)").mark('keyword');

检查这个例子

Why won't you use some highlight js library?

mark.js supports highlighting in tables out of the box

$("table tr td:nth-child(1)").mark('keyword');

Check this example

更多推荐

本文发布于:2023-07-27 08:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:查询结果   WordPress   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!