亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP開發(fā)查詢搜素之關(guān)鍵詞變色

關(guān)鍵詞變色

上一節(jié)我們已經(jīng)基本完成了如何去搜索關(guān)鍵詞,我們接下來對搜索顯示做美化。

在模糊查詢語句中加入下面你的代碼

$row['username'] = str_replace($keywords,'<font color="red">'.$keywords.'</font>',$row['username']);

他的意思是把查詢的名字中查詢的關(guān)鍵詞替換成紅色的字體。

<?php
$keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';
$conn = @mysql_connect("localhost", "root", "root") or die("數(shù)據(jù)庫鏈接錯(cuò)誤");
mysql_select_db("sql", $conn);
mysql_query("set names 'utf8'"); //使用utf-8中文編碼;
//PHP模糊查詢
$sql="SELECT * FROM user WHERE username LIKE '%{$keywords}%'";
$rs= mysql_query($sql);
$users = array();//保存所以得查詢到的用戶
if(!empty($keywords)){
    while ($row=mysql_fetch_assoc($rs)){
        $row['username'] = str_replace($keywords,'<font color="red">'.$keywords.'</font>',$row['username']);
        $users[] = $row;
    }
}
?>

這樣,我們的搜索基本就完成了。

完整代碼展示

<?php
$keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';
$conn = @mysql_connect("localhost", "root", "root") or die("數(shù)據(jù)庫鏈接錯(cuò)誤");
mysql_select_db("sql", $conn);
mysql_query("set names 'utf8'"); //使用utf-8中文編碼;
//PHP模糊查詢
$sql="SELECT * FROM user WHERE username LIKE '%{$keywords}%'";
$rs= mysql_query($sql);
$users = array();//保存所以得查詢到的用戶
if(!empty($keywords)){
 while ($row=mysql_fetch_assoc($rs)){
 $row['username'] = str_replace($keywords,'<font color="red">'.$keywords.'</font>',$row['username']);
 $users[] = $row;
 }

}
?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>查詢器</title>
 <style>


 .textbox {
 width: 355px;
 height: 40px;
 border-radius: 3px;
 border: 1px solid #e2b709;
 padding-left: 10px;
 }

 .su {
 width: 365px;
 height: 40px;
 background-color: #7fbdf0;
 color: white;
 border: 1px solid #666666;

 }
 table{ background-color: #7fbdf0; line-height:25px;}
 th{ background-color:#fff;}
 td{ background-color:#fff; text-align:center}
 </style>
</head>
<body >
<form action="" method="get">
 <p><input type="text" name="keywords" value="" placeholder="請輸入內(nèi)容"/>
 <p><input type="submit" value="查詢"/>
</form>
<?php
if ($keywords){
 echo '<h3>查詢關(guān)鍵詞:<font color="red">'.$keywords.'</font></h3>';
}
if ($users){
 echo '<table width="500" cellpadding="5" >';
 echo '<tr><th>用戶名</th><th>密碼</th><th>郵箱</th><th>性別</th><th>愛好</th>';
 foreach ($users as $key=>$value){
 echo '<tr>';
 echo '<td>'.$value['username'].'</td>';
 echo '<td>'.$value['password'].'</td>';
 echo '<td>'.$value['sex'].'</td>';
 echo '<td>'.$value['email'].'</td>';
 echo '<td>'.$value['hobby'].'</td>';
 echo '</tr>';

 }
}else{
 echo '沒有查詢到相關(guān)用戶';
}
?>
</body>
</html>


Weiter lernen
||
<?php $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : ''; $conn = @mysql_connect("localhost", "root", "root") or die("數(shù)據(jù)庫鏈接錯(cuò)誤"); mysql_select_db("sql", $conn); mysql_query("set names 'utf8'"); //使用utf-8中文編碼; //PHP模糊查詢 $sql="SELECT * FROM user WHERE username LIKE '%{$keywords}%'"; $rs= mysql_query($sql); $users = array();//保存所以得查詢到的用戶 if(!empty($keywords)){ while ($row=mysql_fetch_assoc($rs)){ $row['username'] = str_replace($keywords,'<font color="red">'.$keywords.'</font>',$row['username']); $users[] = $row; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>查詢器</title> <style> .textbox { width: 355px; height: 40px; border-radius: 3px; border: 1px solid #e2b709; padding-left: 10px; } .su { width: 365px; height: 40px; background-color: #7fbdf0; color: white; border: 1px solid #666666; } table{ background-color: #7fbdf0; line-height:25px;} th{ background-color:#fff;} td{ background-color:#fff; text-align:center} </style> </head> <body > <form action="" method="get"> <p><input type="text" name="keywords" class="textbox" value="" placeholder="請輸入內(nèi)容"/> <p><input type="submit" class="su" value="查詢"/> </form> <?php if ($keywords){ echo '<h3>查詢關(guān)鍵詞:<font color="red">'.$keywords.'</font></h3>'; } if ($users){ echo '<table width="500" cellpadding="5" >'; echo '<tr><th>用戶名</th><th>密碼</th><th>郵箱</th><th>性別</th><th>愛好</th>'; foreach ($users as $key=>$value){ echo '<tr>'; echo '<td>'.$value['username'].'</td>'; echo '<td>'.$value['password'].'</td>'; echo '<td>'.$value['sex'].'</td>'; echo '<td>'.$value['email'].'</td>'; echo '<td>'.$value['hobby'].'</td>'; echo '</tr>'; } }else{ echo '沒有查詢到相關(guān)用戶'; } ?> </body> </html>
einreichenCode zurücksetzen