[英]How to highlight user input in mysql search results using php?
我在下面添加了我的代碼,我是php和mysql的新手,並嘗試根據用戶輸入使用php搜索mysql,並且工作得很好。 但是不確定從mysql顯示時如何突出顯示用戶輸入值。
$output = NULL;
if(isset($_POST['submit'])){
$mysqli = new mysqli("localhost","root","","test");
$search = $mysqli->real_escape_string($_POST['search']);
$resultSet = $mysqli->query("SELECT * FROM books WHERE BookContent LIKE '%$search%'");
if($resultSet->num_rows>0){
while($rows = $resultSet->fetch_assoc()){
$BookContent = $rows['BookContent'];
//$output = preg_replace("/($search)/i",'<span class="highlight">$1</span>', $output);
echo $output = "" ."Your search results--> $BookContent"."<br>";
}
}else{
echo $output = "No result" ;
}
}?>
只需將其添加到您的頁面中即可。
CSS:
.highlight{background-color:yellow}
jQuery的:
$("body").highlight("<?php echo $search; ?>");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.