I hava a page with a list of data displayed from database table and a search bar.
When I filter the data by id, the searched data will be highlighted (background color change) but I need it to remain displaying the rest of data.
I managed to change the background color of searched data however if I search the data that is not in table, the Record not found
not displayed.
<?php
$search_keyword = '';
if (isset($_POST['search'])) {
$search_keyword = $_POST['search'];
}
?>
<head></head>
<body>
<form name="searchForm" action="" method="POST">
<input type="text" id="search" name="search" placeholder="Enter Employee ID Search">
</form>
<?php
$sql_search = "";
if (!empty($search_keyword)) {
$sql_search = " WHERE id = '" . $search_keyword . "' ";
}
$sql1 = "SELECT id, name, address FROM employee";
$result = $conn->query($sql1);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
?>
<div class="row">
<div class="employee">
<?php
if ($row["id"] == $search_keyword) {
?>
<div class="red-bg">
<?php
} else {
?>
<div class="white-bg">
<?php
}
?>
<div class="col-md-2"><?php echo $row["id"] ?></div>
<div class="col-md-3"><?php echo $row["name"] ?></div>
<div class="col-md-5"><?php echo $row["address"] ?></div>
</div>
</div>
</div>
</div>
<?php
}
} else {
?>
<div class="row">
<div class="employee">
<div class="white-bg">
<?php echo "Record not found." ?>
</div>
</div>
</div>
<?php
}
?>
</body>
<script>
document.onkeydown = function(evt) {
var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
if (keyCode == 13) {
//your function call here
document.searchForm.submit();
}
}
</script>
If I include the search keyword in query, I can display the Record not found
when searching for data not in table however if I search data that is in table will only display and highlight 1 data.
$sql1 = "SELECT id, name, address FROM employee ". $sql_search ."";
So how do I display all data and highlight searched data that is in table and only display Record not found
when search for data that is not in table?