DataTables Example – Server-side Processing with PHP

In previous post Data Table jQuery Plugin, we have learn what is jQuery datatable plugin and how to use jQuery datatable in your application,now in this tutorial i will describe how to use data table with server side scripting.I am using PHP and MySQL to get records from server side.In this tutorial our aim to get data from MySQL with help of php and passed data to jQuery datatable constructor.

Also Checkout other tutorial of Datatable,

datatable-with-php

There are following steps need to achieve over goal:

Step 1: we will include jquery datatable and jquery library.

Step 2: Create HTML table in your web page.

Step 3: Apply Datatable functionality on our HTML table.

Step 4: Now we will create response.php file and write down below code.

Here we have taken constant array instead of MySQL records, I am assuming you will replace this constant array with your MySQL result set.

  • Rahul

    Nice Tutorial Parvez. I found it very simple than datatable website. I’m working on bootstrap 3 with datatables with huge dataset coming from DB, I just wanted it to be from server rather than sending the whole dataset to datatables and leave this to it. But having huge dataset (>10K records) taking little time. Can you please provide the code to get the data from server with pagination, sorting and search. Thanks a ton in advance..

    • Hi Rahul,
      sure,I will write new artcile for this.

    • rajat

      Hey , I have a similar problem , did you find the solution . Can you please send it to redlives26@gmail.com.
      Thanks!

      • Hi,
        In step 4 i have mensioned static array,

        $data = array(
        array(‘Name’=>’parvez’, ‘Empid’=>11, ‘Salary’=>101),
        array(‘Name’=>’alam’, ‘Empid’=>1, ‘Salary’=>102),
        array(‘Name’=>’phpflow’, ‘Empid’=>21, ‘Salary’=>103) );

        below i am providing roughly idea:
        $sql =”select * from studens”;

        $data = mysql_fetch_assoc($sql),

        now you can get associative array form your database.

  • Satya

    Hello Parvez, I am Satya a beginner in Datatables, I am working in such a situation that I need to generate datatable on button click and it should get the data from the database using Joins, I tired doind few cases but I get Invalid json format, so can you suggest me how to do that please ?

    • you can take reference from downloaded sorce code,otherwise you need to sent source code with mysql.

  • Jesus Neira Guio

    Excelente tutorial.

  • Vladka

    Hi! Thanks a lot, this is really helpfull for my project. I am a Computer Engineering student, still learning about this kind of things and I bookmarked this website for reference. Once again thanks a lot!!

  • Chandra Sihombing

    Hi Parvez,

    Love your script much, i has same question with Rahul. There is almost 10K dataset in my database and took so long to load into dataset. I’m waiting your next tutorial based on this case.

    Cheers

    • Thanks for your appreciation, you can use cache to load first time data and again use cache data for this.

  • veerla srinivasarao

    i want to datatables with records form backend mysql thay why please post the data using mysql table

    • In this post you can set static records with your mysql array records set.

  • Nabih

    Hi,

    I have used this method, it is working fine, but i have few questions:
    1- If i need to add static values to the last column, example: Delete icon, to delete the record
    2- Color the line based on a certain value
    3- Instead of Previous, Next to use <>
    4- Pass a certain value to filter the select in the server side
    5- Align the pagination with the Showing number of entries

    Thank you

    • The point 2,3 and 5 can be handle by css and rest of them can be found in datatable dos easily,if you not find let us know,i ll share link or tutorial for them.

  • winshelo ST-LOUIS

    Hi, this is a good example thank you a lot,
    could you please give me and example with the Mysql records
    Regards

    • the source code have used mysql datasource to get results and passed to datatable.

  • winshelo ST-LOUIS

    Hi, thank you for the example, i would like to have and example with Mysql.

    Regards

  • subhash

    Hi this is good example of static value can you please give me example of Mysql record i had error
    json data from server cannot be parse…

    • I hv posted using mysql as well

  • Hi Subhash,
    You can use below CDN,

    still getting issue , send the code on phpflow@gmail.com

  • alex

    Very very very good example, thanks a lot for it