• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

PHP - Display MySQLi Result In DataTable

3serve

Level-Up Legend
3 Rep
0
0
0
Rep
0
3 Vouches
0
0
0
Vouches
0
Posts
107
Likes
166
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
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.
2019-10-18_23_43_30-localhost_127.0.0.1_db_data_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_data_table"

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

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. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `member`"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  48. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  49. ?>
  50. <tr>
  51. <td><?php

    echo

    $fetch

    [

    'firstname'

    ]

    ?>

    </td>
  52. <td><?php

    echo

    $fetch

    [

    'lastname'

    ]

    ?>

    </td>
  53. <td><?php

    echo

    $fetch

    [

    'address'

    ]

    ?>

    </td>
  54. </tr>
  55. <?php
  56. }
  57. ?>
  58. </tbody>
  59. </table>
  60. </div>
  61. </div>


  62. <script src="js/jquery-3.2.1.min.js"></script>
  63. <script src="js/bootstrap.js"></script>
  64. <script src = "js/jquery.dataTables.js"></script>
  65. </body>
  66. </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. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  4. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  5. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  6. $address

    =

    $_POST

    [

    'address'

    ]

    ;

  7. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `member` VALUES('', '$firstname

    ', '$lastname

    ', '$address

    ')"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;

  8. header

    (

    "location: index.php"

    )

    ;
  9. }
  10. ?>

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!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,508

356,407

356,420

Top