After employee records has been added to the database, you should provide the user a way to SEARCH for a particular record or records. While you could create a separate page to perform this task, we are going to add the functionality to the EMPLOYEE MASTER PAGE instead.
The code that will be created will perform the following tasks:
Static Search input field has already been added to EMPLOYEE MASTER PAGE (
<h2 class="ui-body ui-body-a ui-corner-all">EMPLOYEE MASTER PAGE</h2>
<a data-role = "button" href = "add_page.php" data-inline = "true"
class = "ui-btn ui-corner-all ui-icon-user ui-btn-icon-left">ADD EMPLOYEE</a>
<input type='search' maxlength='25' placeholder="Enter a search word and press ENTER key">
<div data-role="content">
<form name='frmSearch' action='' method='post'>
<h2 class="ui-body ui-body-a ui-corner-all">EMPLOYEE MASTER PAGE</h2>
...
...
...
... </form>
</div>
We will add two scripts to create the search function and the pagination nav at the bottom of the ListView.
A few changes need to be made to create the search functionality.
<!-- SQL SELECT QUERY ------------------------------------------- -->
<?php
$pdo_statement = $pdo_conn->prepare("SELECT ID, CONCAT(FirstName, ' ' , LastName) AS
FullName, Department, Image FROM employees ORDER BY FullName ASC");
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();
?>
<!-- SQL SELECT QUERY ------------------------------------------- -->
<?php
$search_keyword = '';
if(!empty($_POST['search']['keyword'])) {
$search_keyword = $_POST['search']['keyword'];
}
$sql = "SELECT ID, CONCAT(FirstName, ' ' , LastName) AS FullName, Title, Image FROM employees WHERE FirstName LIKE :keyword OR LastName LIKE :keyword OR Title LIKE :keyword ORDER BY FullName ASC";
$pdo_statement = $pdo_conn->prepare($sql);
$pdo_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();
?>
The static content of the <input> tag need to be made more dynamic with some attributes.
<h2 class="ui-body ui-body-a ui-corner-all">EMPLOYEE MASTER PAGE</h2>
<a data-role = "button" href = "add_page.php" data-inline = "true"
class = "ui-btn ui-corner-all ui-icon-user ui-btn-icon-left">ADD EMPLOYEE</a>
<input type='search' name='search[keyword]' value="<?php echo $search_keyword; ?>"
id='keyword' maxlength='25' placeholder="Enter a search word and press ENTER key">
CODE EXPLANATION:
- The search[keyword] resolves to the name of the <input> field.
- The <?php echo $search_keyword; ?> resolves to the value of the <input> field.
It is best practice to implement a search engine if there are a lot of records being retrieved from the database. See the Enhancement tab for details.