我有一个搜索数据库的页面。我希望获取用户在搜索框中输入的数据,以便在搜索结果中突出显示。
来自用户的数据存储在变量' $ criteria'中,以及我想要突出显示的那个变量的所有内容。
我的PHP看起来如下:
<?php
session_start();
if ($_SESSION['loggedin'] != 1) {
header("Location: ../");
}
include("../php_includes/connection.php");
$queryErrorMessage = "";
?>
<html>
<head>
<title>Search Tasks</title>
<link rel="stylesheet" href="../css/bootstrap.min.css">
<script src="../js/bootstrap.min.js"></script>
<link rel="stylesheet" href="../css/style.css">
</head>
<body>
<?php include("header.php"); ?>
<?php include("sidebar.php"); ?>
<div class="row">
<div class="col-md-9">
<div class="well well-lg">
<h3>Search Tasks</h3>
<hr>
<form method="post" action="">
<div class="input-group">
<input name="criteria" type="text" class="form-control" placeholder="What would you like to search for?...">
<div class="input-group-btn">
<button class="btn btn-default" type="submit">
Submit
</button>
</div>
</div>
</form>
<?php
//Run section of code if the POST criteria is provided for "criteria"
if(isset($_POST['criteria'])){
$criteria = mysqli_real_escape_string($db,$_POST['criteria']);
//If the user does not provide criteria to search, an error will be displayed.
if($criteria == ""){
$queryErrorMessage = "You did not provide any criteria";
GoTO errorMsg;
}
//Prepare sql statement checking each column with the criteria that is provided.
$sql = "SELECT * FROM taskinfo WHERE clientCompany LIKE '%".$criteria."%' OR clientName LIKE '%".$criteria."%' OR email LIKE '%".$criteria."%' OR contactNo LIKE '%".$criteria."%' OR details LIKE '%".$criteria."%'";
//Run sql query storing the records in $result
if($result = mysqli_query($db, $sql)){
if(mysqli_num_rows($result)>0){
//Get each row and put it into the array $row
echo '<h4>Displaying results for search criteria: <b>'.$criteria.'</b></h4>';
while($row = mysqli_fetch_array($result)){
$queryErrorMessage = "";
//Display the query results
?>
<div class="panel panel-default">
<div class="panel-body">
<h6><b>Task Details:</b></h6>
<?php echo $row['details'];?>
<h6><b>Task Information</b></h6>
<ul>
<li>Client Name: <?php echo $row['clientName'];?></li>
<li>Email: <a href="mailto:<?php echo $row['email'];?>"><?php echo $row['email'];?></a></li>
<li>Phone: <?php echo $row['contactNo'];?></li>
<li>Posted by <b><?php echo $row['postedBy'];?></b></li>
<li>Posted On: <b><?php echo $row['timeAdded']; ?></b></li>
</ul>
</div>
</div>
<?php
}
} else {
$queryErrorMessage = "No records found with your criteria.";
}
} else {
$queryErrorMessage = "Failed to perform query.";
}
}
//Check if there is an error message
//If true, echo the error to the user.
//
//Section of code is called errorMsg to
//Allow it to be called in the code above.
errorMsg:
if($queryErrorMessage != ""){
echo 'An Error Occurred: ';
echo $queryErrorMessage;
}
?>
</div>
</div>
</div>
</body>
</html>
如果有人可以提供帮助,我们将非常感激。
答案 0 :(得分:0)
这可能会有所帮助
<?php echo preg_replace_callback('/'.$criteria.'/i', function($matches){
return '<strong>' . $matches[0] . '</strong>';
}, $row['clientName']); ?>
它会使内容中的搜索关键字变为粗体