在mysql PHP搜索中突出显示搜索文本 [英] Highlight search text in mysql php search

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

问题描述

这是我拥有的脚本,可以很好地搜索我的数据库. 我有一个 form.html ,用户在搜索框中输入关键字.

hi this is the script i have, it works well to search my database. i have a form.html where the user inputs the keyword in a search box.

<form method="get" action="form.php">
<input type="text" name="searchterm" title="Enter your search term here" value="Enter your search term here" class="searchbox" />
<input type="submit" name="search" title="Search Now! "value="Search" class="searchbutton" />
</form>

我想突出显示搜索结果中的搜索文字.

非常感谢

form.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<?php

// include MySQL-processing classes

require_once 'mysql.php'; 

try{

// connect to MySQL

$db=new MySQL(array('host'=>'','user'=>'','password'=>'','database'=>''));

$searchterm=$db->escapeString($_GET['searchterm']);

$result=$db->query("SELECT * FROM BookPage WHERE (PageText) like \"%$searchterm%\" ");

if(!$result->countRows()){

echo '<div class="maincontainer"><h2>No results were found. Go back and try a new search.</h2></div>'."n";

}

else{

// display search results

echo '<div class="maincontainer"><h2>Your search criteria
returned '.$result->countRows().' results.</h2>'."n";

while($row=$result->fetchRow()){

echo '<div class="rowcontainer"><p><strong>Book Id:
</strong>'.$row['BookId'].'<p><p><strong>Page Id:
</strong>'.$row['PageId'].'</p><p><strong>Page Text:
</strong>'.$row['PageText'].'</p></div>'."n"; 

}

}

echo '</div>';

}

catch(Exception $e){

echo $e->getMessage();

exit();

}

?>
</body>
</html>

推荐答案

尝试一下:

$searchvalue = implode('<span style="color:green;font-weight:800;">'.$_GET['searchterm'].'</span>',explode($_GET['searchterm'],$row['PageText']));
echo '<div class="rowcontainer"><p><strong>Book Id:
</strong>'.$row['BookId'].'<p><p><strong>Page Id:
</strong>'.$row['PageId'].'</p><p><strong>Page Text:
</strong>'.$searchvalue.'</p></div>'."n"; 

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

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