Search
To add a search function, we need a form and we need to modify our SQL syntax.
To begin, we will add a line of PHP code immediately following our h1 tag. This line will set our “keyword” variable. We will use the “isset()” function to determine if the $_GET[‘keyword’] variable is not empty, which indicates that the search form has been submitted. If this is the case, we will set the $keyword variable to the value of the submitted form’s GET parameter. If the $_GET[‘keyword’] variable is empty, we will set the $keyword variable to an empty string.
Then, we add a form as our search bar. We put index.php as its action and GET as its method so that it will direct back to the same page with GET parameters that will be saved as $keyword with the line above.
<?php $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : ''; ?> <div> <form action="index.php" method="GET"> <input type="text" id="keyword" name="keyword" value="<?php echo $keyword ?>"> <input type="submit" value="Search"> </form> </div>
Our SQL statement has been updated to include the LIKE clause with % symbols placed before and after the keyword. This modification allows the statement to retrieve every row that contains the keyword anywhere within the name. For instance, a keyword like ‘al’ would return results such as ‘alvin’, ‘albert’, and ‘calvin’.
When the search keyword is an empty string (”), all rows will match the search criteria, as every field will contain an empty string as a substring. Consequently, the search results will include all the records in the database.
In the currect CRUD application, modify this code by appending “WHERE name LIKE ‘%$keyword%'”.
$sql = "SELECT * FROM contacts'";
So that it looks like the following:
$sql = "SELECT * FROM contacts WHERE name LIKE '%$keyword%'";
If you searched for a keyword, it will return the results as below. Also notice how the GET parameter for the keyword also shows in the URL.
Below is the full index.php code after search functionality is added.
<?php include('connect.php'); include('head.php'); ?> <h1>Contact Manager</h1> <?php $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : ''; ?> <div> <form action="index.php" method="GET"> <input type="text" id="keyword" name="keyword" value="<?php echo $keyword ?>"> <input type="submit" value="Search"> </form> </div> <a href="create.php" class="btn btn-primary mb-3">Create New Contact</a> <table class="table"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Phone</th> <th>Notes</th> <th>Actions</th> </tr> </thead> <tbody> <?php // retrieving data from database $sql = "SELECT * FROM contacts WHERE name LIKE '%".$keyword."%'"; $row = $connection->prepare($sql); $row->execute(); $contacts = $row->fetchAll(); ?> <?php foreach ($contacts as $contact) { ?> <tr> <td><?php echo $contact['id']; ?></td> <td><?php echo $contact['name']; ?></td> <td><?php echo $contact['email']; ?></td> <td><?php echo $contact['phone']; ?></td> <td><?php echo $contact['notes']; ?></td> <td> <!-- redirecting to edit page with the id as identifier --> <a href="edit.php?id=<?php echo $contact['id']; ?>" class="btn btn-warning">Edit</a> <!-- deleting a data with id as identifier --> <a onclick="return confirm('Delete this contact?')" href="delete.php?id=<?php echo $contact['id']; ?>" class="btn btn-danger">Delete</a> </td> </tr> <?php } ?> </tbody> </table> <?php include('foot.php') ?>