Project structure:
Start the search: Search keywords here ("big", "this")
Search results: Highlight
Database structure required for the project:
Implementation code:
conn.php
Copy code The code is as follows:
$conn = @ mysql_connect("localhost", "root", "") or die("Database link error");
mysql_select_db("form", $conn);
mysql_query("set names 'gbk'");
?>
searchAndDisplayWithColor.php
Copy code Code As follows:
include 'conn.php';
?>
cellspacing="1" bgcolor="#add3ef">
//Only perform related searches when the keyword is not empty
if($_GET['keyWord']){
//Separate the keywords with spaces
$key=explode(' ', $_GET[keyWord]);
$sql="select * from message where title like '$key[0]' or title like '$key[1]' or content like '$key[0]' or content like '%$key[1]%'";
$query=mysql_query($sql);
while ($row=mysql_fetch_array($query)){
//Replace keyword, And highlight the keyword
$row[title]=preg_replace("/$key[0]/i", "$key[0]", $row[title]);
$row[title]=preg_replace("/$key[0]/i", "$ key[1]", $row[title]);
$row[content]=preg_replace("/$key[0]/i", "$key[0]", $row[content]);
$row[content]=preg_replace("/$key[1]/ i", "$key[1]", $row[content]);
?>
Title: =$row[title]?> User: =$row[user] ?>
|
< td>Content:=$row[content]?>
Publication date:=$row[lastdate]?>
|
}
?>
Note: In this small program, there is one shortcoming, only You can search for two keywords at the same time, separated by a space " ". If you only search for one keyword, such as: "big"
, garbled characters will appear when displayed... ^|_|^, this is due to the following The result of the code:
Copy the code The code is as follows:
//Separate the keywords with spaces
$ key=explode(' ', $_GET[keyWord]);
If you want to improve, you have to make a judgment later here.
http://www.bkjia.com/PHPjc/325331.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325331.htmlTechArticleProject structure: Start search: Search keywords here ("big" "this") Search results: Highlighted Database structure required for the project: Implementation code: conn.php Copy the code as follows: ?p...