PHP - Dynamically Change Table Properties

In this tutorial we will create a Dynamically Change Table Properties using PHP. This code will dynamically change the user properties that comes with different functionalities The code itself use DataTable to contain the data with a interactive table design.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_table, after that click Import then locate the database file inside the folder of the application then click ok. https://www.sourcecodester.com/sites/default/files/2019-03-18_21_47_29-localhost_127.0.0.1_db_table_phpmyadmin_4.8.3.png

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_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 - Dynamically Change Table Properties</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <button type="button" class="btn btn-success pull-left" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add member</button>
  19. <form method="POST" action="">
  20. <div class="pull-right form-inline">
  21. <select name="table" class="form-control" required="required">
  22. <option value="">Select an option</option>
  23. <option value="plain">Plain</option>
  24. <option value="dtable">Data Table</option>
  25. </select>
  26. <button class="btn btn-primary" name="change">Change</button>
  27. </div>
  28. </form>
  29. <br /><br /><br />
  30. <?php include'data.php'?>
  31. </div>
  32. <div class="modal fade" id="form_modal" aria-hidden="true">
  33. <div class="modal-dialog">
  34. <div class="modal-content">
  35. <form method="POST" action="save.php">
  36. <div class="modal-header">
  37. <h3 class="modal-title">Add member</h3>
  38. </div>
  39. <div class="modal-body">
  40. <div class="col-md-2"></div>
  41. <div class="col-md-8">
  42. <div class="form-group">
  43. <label>Firstname</label>
  44. <input type="text" class="form-control" name="firstname" required="required"/>
  45. </div>
  46. <div class="form-group">
  47. <label>Lastname</label>
  48. <input type="text" class="form-control" name="lastname" required="required"/>
  49. </div>
  50. <div class="form-group">
  51. <label>Age</label>
  52. <input type="number" min="1" class="form-control" name="age" required="required"/>
  53. </div>
  54. <div class="form-group">
  55. <label>Address</label>
  56. <input type="text" class="form-control" name="address" required="required"/>
  57. </div>
  58. </div>
  59. </div>
  60. <br style="clear:both;"/>
  61. <div class="modal-footer">
  62. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  63. <button name="save" class="btn btn-primary"><span class='glyphicon glyphicon-save'></span> Save</button>
  64. </div>
  65. </form>
  66. </div>
  67. </div>
  68. </div>
  69. <script src="js/jquery-3.2.1.min.js"></script>
  70. <script src="js/bootstrap.js"></script>
  71. <script src = "js/jquery.dataTables.js"></script>
  72. <script type="text/javascript">
  73. $(document).ready(function(){
  74. $('#table').DataTable();
  75. });
  76. </script>
  77. </body>
  78. </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. $age = $_POST['age'];
  8. $address = $_POST['address'];
  9.  
  10. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$age', '$address')") or die(mysqli_error());
  11.  
  12. header("location: index.php");
  13. }
  14. ?>

Creating the Main Function

This code contains the main function of the application. This code will change the table properties when clicked. To make this just copy and write these block of codes below inside the text editor, then save it as data.php
  1. <?php
  2. if(ISSET($_POST['change'])){
  3. if($_POST['table'] == "plain"){
  4. ?>
  5. <table class="table table-bordered">
  6. <thead class="alert-info">
  7. <tr>
  8. <th>Firstname</th>
  9. <th>Lastname</th>
  10. <th>Age</th>
  11. <th>Address</th>
  12. </tr>
  13. </thead>
  14. <tbody>
  15. <?php
  16. require 'conn.php';
  17.  
  18. $query = mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  19. while($fetch = mysqli_fetch_array($query)){
  20. ?>
  21. <tr>
  22. <td><?php echo $fetch['firstname']?></td>
  23. <td><?php echo $fetch['lastname']?></td>
  24. <td><?php echo $fetch['age']?></td>
  25. <td><?php echo $fetch['address']?></td>
  26. </tr>
  27. <?php
  28. }
  29. ?>
  30. </tbody>
  31. </table>
  32. <?php
  33. }else if($_POST['table'] == "dtable"){
  34. ?>
  35. <table id="table" class="table table-bordered">
  36. <thead class="alert-info">
  37. <tr>
  38. <th>Firstname</th>
  39. <th>Lastname</th>
  40. <th>Age</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['age']?></td>
  55. <td><?php echo $fetch['address']?></td>
  56. </tr>
  57. <?php
  58. }
  59. ?>
  60. </tbody>
  61. </table>
  62. <?php
  63. }
  64. }else{
  65. ?>
  66. <table class="table table-bordered">
  67. <thead class="alert-info">
  68. <tr>
  69. <th>Firstname</th>
  70. <th>Lastname</th>
  71. <th>Age</th>
  72. <th>Address</th>
  73. </tr>
  74. </thead>
  75. <tbody>
  76. <?php
  77. require 'conn.php';
  78.  
  79. $query = mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  80. while($fetch = mysqli_fetch_array($query)){
  81. ?>
  82. <tr>
  83. <td><?php echo $fetch['firstname']?></td>
  84. <td><?php echo $fetch['lastname']?></td>
  85. <td><?php echo $fetch['age']?></td>
  86. <td><?php echo $fetch['address']?></td>
  87. </tr>
  88. <?php
  89. }
  90. ?>
  91. </tbody>
  92. </table>
  93. <?php
  94. }
  95. ?>
There you have it we successfully created Dynamically Change Table Properties 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