Php

Simple Example Bootgrid (Server Side) with PHP, MySQL and Ajax

This tutorial help to understand Bootgrid listing with PHP, MySQL Using Ajax.We will convert a simple HTML table into a feature table with features such as table data searching, pagination, column sorting, and many more.

Bootgrid is a fantastic grid plugin for displaying results. Bootgrid grid is a bootstrap-specific grid control. Bootgrid Lightweight, Cross Browser Support (IE, Firefox, Chrome, Safari, Opera), and HTML5 support are among its many features.

You can use bootgrid to generate a listing using two methods.

You can also check other tutorial for Grid,

Bootgrid is an extremely well-built jQuery grid plugin that is used to transform a simple HTML table into a powerful grid with functionality such as inserting, updating, and deleting records from the table, table column sorting, pagination, and searching data on the server side.

Option 1: Bootgrid Example with Client Side

Client-side means you offer pagination, sorting, and searching by loading entire data sets at once. This will work fine for a small set of records, but if you have a large set of records, you will run into performance issues because fetching large amounts of data from the server side will take time to process and load data into the table.

Bootgrid Example of Pagination, Sorting and Searching Using Client Side Data Binding

There is no special configuration required to use bootgrid on a table. You must retrieve all records from the database and bind them to the table using rows via HTML or use the jQuery append method to dynamically append rows to the table body. Finally, use the data-toggle="bootgrid" attribute in your table to initialize bootgrid, as shown below.

<table id="employee_grid" class="table table-condensed table-hover table-striped" width="100%" cellspacing="0" data-toggle="bootgrid">
        ...
</table>

data-toggle use to initialize bootgrid on the table.

Related Post

Option 2: Bootgrid (Server Side) With PHP, MySQL and Ajax

Server-side processing will come into the picture when you have a large set of data. Server side data will fetch data in chunk, so data overhead of your application will improve and get better performance of application. We need the ajax option to true and pass an URL to the url option in the bootgrid method.

There are Following files will participate in Bootgrid with PHP, MySql and ajax

  • index.php: This file is responsible to create html table and pagination.
  • connection.php: This file is used to create a connection with mysql.
  • response.php This file is responsible to fetch data from the database using mysql query and return json data to index.php.
  • dist : This is a folder, which is used to keep all library files.

Simple Demo Bootgrid (Server Side) with PHP, MYsql and ajax

Step 1: Include js and css file into index.php.

<script src="dist/jquery-1.11.1.min.js"></script>
<script src="dist/bootstrap.min.js"></script>
<script src="dist/jquery.bootgrid.min.js"></script>

Step 2: Define html table and call data-toggle="bootgrid" attribute

<table id="employee_grid" class="table table-condensed table-hover table-striped" width="100%" cellspacing="0" data-toggle="bootgrid">
 <thead>
  <tr>
   <th data-column-id="id" data-type="numeric">Empid</th>
   <th data-column-id="employee_name">Name</th>
   <th data-column-id="employee_salary">Salary</th>
   <th data-column-id="employee_age">Age</th>
  </tr>
 </thead>
</table>

Step 3: Define ajax request to fetch data from server-side.

$("#employee_grid").bootgrid({
        ajax: true,
        post: function ()
        {
            /* To accumulate custom parameter with the request object */            return {
                id: "b0df282a-0d67-40e5-8558-c9e93b7befed"
            };
        },
        url: "response.php",
        formatters: {
            
        }
});

Here #employee_grid is table id where i am implementing bootgrid and url: "response.php" is the target service or file to call on each pagination request.

Step 4: response.php will contain all server-side processing logic and code.

$totalRecords = mysqli_num_rows($queryTot);

    $queryRecords = mysqli_query($conn, $sqlRec) or die("error to fetch employees data");

    //iterate on results row and create new index array of data
    while( $row = mysqli_fetch_assoc($queryRecords) ) { 
        $data[] = $row;
}

$json_data = array(
"current" =&gt; intval( $params['current'] ),
"rowCount" =&gt; 10,
"total" =&gt; intval( $totalRecords ),
"rows" =&gt; $data // total data array
);

echo json_encode($json_data); // send data as json format

Conclusion :

Bootgrid is very simple and easy to use with php and MySQL.We can use one-time data records(Client side sorting, searching and pagination) as well as server-side (sorting, searching and pagination) for large data sets.

Demo & Download Code Of Bootgrid with PHP,Mysql and Ajax

Please feel free to send queries to me using below comment section.

View Comments

  • I want to thanks to samuel, who made some changes for laravel Framework,
    //Handles Sort query string sent from Bootgrid
    if (isset($params['sort']) && is_array($params['sort']) )
    {
    $order_by="";
    foreach($params['sort'] as $key=> $value)
    $order_by.=" $key $value";

    $where .="ORDER BY ".$order_by." ";
    }

  • This article is very helpful, I really appreciate.
    but how to put the Edit and Delete button and enter the KEY ID on each line for the next action ?
    Please explain to me and give some examples.
    Thank You.

    • You can add/edit/delete button like datatable example,anyways i will share new article with this requirement.

  • I've followed the above tutorial and also the below JQuery Bootgrid documentation in order to create a dataGrid for my DB.

    - http://www.abrandao.com/2014/11/bootstrap-bootgrid-with-php-pdo-server-script/

    - http://www.jquery-bootgrid.com/Documentation#column

    Unfortunately, I'm not able to display the Command buttons on the code below, when trying to display them using "formatters":

    Here is my question submitted on stackoverflow.com

    http://stackoverflow.com/questions/39321000/jquery-bootgrid-not-displaying-command-buttons

    Any help would be really appreciated.

    Thank you....

  • i am using below bootgrid config code,Its working fine.
    $( document ).ready(function() {
    var grid = $("#employee_grid").bootgrid({
    ajax: true,
    post: function ()
    {
    /* To accumulate custom parameter with the request object */
    return {
    id: "b0df282a-0d67-40e5-8558-c9e93b7befed"
    };
    },

    url: "response.php",
    formatters: {
    "commands": function(column, row)
    {
    return " " +
    "";
    }
    }
    }).on("loaded.rs.jquery.bootgrid", function()
    {
    /* Executes after data is loaded and rendered */
    grid.find(".command-edit").on("click", function(e)
    {
    alert("You pressed edit on row: " + $(this).data("row-id"));
    }).end().find(".command-delete").on("click", function(e)
    {
    alert("You pressed delete on row: " + $(this).data("row-id"));
    });
    });
    });

    i am using bootstrap icon classes

    • Hello Dear, you were right.... I had another object with the same name as my command column, hence it won't display command columns..

      Thank you so much for your quick answer.

  • i have share full source code, you need to create test folder, dump mysql file in test db.You need put source code into xampp like, xampp/htdocs/bootgrid, now open localhost/bootgrid, it will work perfectly.

    • uncomment below code into index.php,

      $.post('response.php', { id: $(this).data("row-id"), action:'delete'}
      , function(){
      // when ajax returns (callback),
      $("#employee_grid").bootgrid('reload');
      });

  • Hi,
    I had this working fine on php mysql, but now I have to migrate this code to be able to run on sql server, I managed do all the php stuff but I am stuck with the response.php code converting from mysql to sql server, do you have any conversion done?

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

2 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories