PHP - Simple Table Data Highlighting using jQuery

In this tutorial we will create a Simple Table Data Highlighting using jQuery. This code will highlight a specific row or column base on the mouse pointer position.The code itself use jQuery script to modify the css content of each element dynamically. This is a user-friendly kind of program feel free user it in your program. We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can retrieve DOM and manipulate each properties of an element based on different criteria such as id, class, etc.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_highlight, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn = mysqli_connect("localhost", "root", "", "db_highlight");
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.                 <style>
  7.                 .highlight-row {
  8.                         background-color:maroon;
  9.                         cursor:pointer;
  10.                         color:#FFF;
  11.                 }
  12.                 .highlight-col {
  13.                         background-color:indigo;
  14.                         color:#FFF;
  15.                 }
  16.                 </style>
  17.         </head>
  18. <body>
  19.         <nav class="navbar navbar-default">
  20.                 <div class="container-fluid">
  21.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  22.                 </div>
  23.         </nav>
  24.         <div class="col-md-3"></div>
  25.         <div class="col-md-6 well">
  26.                 <h3 class="text-primary">PHP - Simple Table Data Highlighting using jQuery</h3>
  27.                 <hr style="border-top:1px dotted #ccc;"/>
  28.                 <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add User</button>
  29.                 <br /><br />
  30.                 <table class="table table-bordered">
  31.                         <thead class="alert-info">
  32.                                 <tr>
  33.                                         <th class="header">Firstname</th>
  34.                                         <th class="header">Lastname</th>
  35.                                         <th class="header">Address</th>
  36.                                 </tr>
  37.                         </thead>
  38.                         <tbody>
  39.                                 <?php
  40.                                         require 'conn.php';
  41.                                        
  42.                                         $query = mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  43.                                         while($fetch = mysqli_fetch_array($query)){
  44.                                 ?>
  45.                                 <tr class="table-row">
  46.                                         <td><?php echo $fetch['firstname']?></td>
  47.                                         <td><?php echo $fetch['lastname']?></td>
  48.                                         <td><?php echo $fetch['address']?></td>
  49.                                 </tr>
  50.                                 <?php
  51.                                         }
  52.                                 ?>
  53.                         </tbody>
  54.                 </table>
  55.         </div>
  56. <div class="modal fade" id="form_modal" aria-hidden="true">
  57.         <div class="modal-dialog">
  58.                 <div class="modal-content">
  59.                         <form method="POST" action="save_user.php">
  60.                                 <div class="modal-header">
  61.                                         <h3 class="modal-title">Add User</h3>
  62.                                 </div>
  63.                                 <div class="modal-body">
  64.                                         <div class="col-md-2"></div>
  65.                                         <div class="col-md-8">
  66.                                                 <div class="form-group">
  67.                                                         <label>Firstname</label>
  68.                                                         <input type="text" class="form-control" name="firstname" required="required"/>
  69.                                                 </div>
  70.                                                 <div class="form-group">
  71.                                                         <label>Lastname</label>
  72.                                                         <input type="text" class="form-control" name="lastname" required="required"/>
  73.                                                 </div>
  74.                                                 <div class="form-group">
  75.                                                         <label>Address</label>
  76.                                                         <input type="text" class="form-control" name="address" required="required"/>
  77.                                                 </div>
  78.                                         </div>
  79.                                 </div>
  80.                                 <br style="clear:both;"/>
  81.                                 <div class="modal-footer">
  82.                                         <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  83.                                         <button name="save" class="btn btn-primary" ><span class="glyphicon glyphicon-save"></span> Save</button>
  84.                                 </div>
  85.                         </form>
  86.                 </div>
  87.         </div>
  88. </div>
  89. <script src="js/jquery-3.2.1.min.js"></script> 
  90. <script src="js/bootstrap.js"></script>
  91. <script src="js/script.js"></script>
  92. </body>
  93. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the data inputs to the MySQLi database server. To do that just copy and write this block of codes inside the text editor, then save it as save_user.php.
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_POST['save'])){
  5.                 $firstname = $_POST['firstname'];
  6.                 $lastname = $_POST['lastname'];
  7.                 $address = $_POST['address'];
  8.                
  9.                 mysqli_query($conn, "INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  10.                
  11.                 header("location: index.php");
  12.         }
  13. ?>

Creating the Main Function

This code contains the main function of the application. This code will highlight the table data whenever the mouse pointer is hover on it. To make this just copy and write these block of codes below inside the text editor, then save it as script.js inside the js directory.
  1. $(document).ready(function() {
  2.     $('.table-row').hover(function() {            
  3.                 $(this).addClass('highlight-row');
  4.     }, function() {
  5.                 $(this).removeClass('highlight-row');
  6.     });
  7.    
  8.     $("th").hover(function() {
  9.                 var index = $(this).index();
  10.                 $(this).css('cursor', 'pointer');
  11.                 $("th.header, td").filter(":nth-child(" + (index+1) + ")").addClass("highlight-col");
  12.                 $("th.header").filter(":nth-child(" + (index+1) + ")").attr('class', 'alert-info');
  13.         }, function() {
  14.                 var index = $(this).index();
  15.                 $("th.header, td").removeClass("highlight-col");
  16.         });
  17. });
There you have it we successfully created Simple Table Data Highlighting using jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment