PHP - Display MySQLi Result In DataTable

In this tutorial we will create a Display MySQLi Result In DataTable using PHP. This code will automatically change table properties into a interactive advance table management. The code use DataTable to contain the data with a interactive table design by just binding the id of the table.This is a user-friendly kind of program feel free to modify it. We will use DataTable as a JavaScript framework that handle and manage the data within the table. It has a wealth of options which can be used to configure that obtain and process the data in a complex table.

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 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_data_table, 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_data_table");
  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.                 <link rel = "stylesheet" type = "text/css" href = "css/jquery.dataTables.css" />
  7.         </head>
  8. <body>
  9.         <nav class="navbar navbar-default">
  10.                 <div class="container-fluid">
  11.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">PHP - Display MySQLi Result In DataTable</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-4">
  19.                         <form method="POST" action="save.php">
  20.                                 <div class="form-group">
  21.                                         <label>Firstname</label>
  22.                                         <input type="text" class="form-control" name="firstname" required="required"/>
  23.                                 </div>
  24.                                 <div class="form-group">
  25.                                         <label>Lastname</label>
  26.                                         <input type="text" class="form-control" name="lastname" required="required"/>
  27.                                 </div>
  28.                                 <div class="form-group">
  29.                                         <label>Address</label>
  30.                                         <input type="text" class="form-control" name="address" required="required"/>
  31.                                 </div>
  32.                                 <center><button name="save" class="btn btn-primary">Save</button></center>
  33.                         </form>
  34.                 </div> 
  35.                 <div class="col-md-8">
  36.                         <table id="table" class="table table-bordered">
  37.                                 <thead class="alert-info">
  38.                                         <tr>
  39.                                                 <th>Firstname</th>
  40.                                                 <th>Lastname</th>
  41.                                                 <th>Address</th>
  42.                                         </tr>
  43.                                 </thead>
  44.                                 <tbody>
  45.                                         <?php
  46.                                                 require 'conn.php';
  47.                                                
  48.                                                 $query = mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  49.                                                 while($fetch = mysqli_fetch_array($query)){
  50.                                         ?>
  51.                                                 <tr>
  52.                                                         <td><?php echo $fetch['firstname']?></td>
  53.                                                         <td><?php echo $fetch['lastname']?></td>
  54.                                                         <td><?php echo $fetch['address']?></td>
  55.                                                 </tr>
  56.                                         <?php
  57.                                                 }
  58.                                         ?>
  59.                                 </tbody>
  60.                         </table>
  61.                 </div>
  62.         </div>
  63.        
  64.  
  65. <script src="js/jquery-3.2.1.min.js"></script>
  66. <script src="js/bootstrap.js"></script>
  67. <script src = "js/jquery.dataTables.js"></script>
  68. </body>
  69. </html>

Creating PHP Query

This code contains the php query of the application. This code save the user 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.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 `member` 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 change the table properties when applied as a javascript source.
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3.         $('#table').DataTable({
  4.                 "order": [[2, "asc"]]
  5.         });
  6. });
  7. </script>
There you have it we successfully created Display MySQLi Result In DataTable using PHP. 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