Live Data Search with Pagination in PHP using Ajax

Webslesson

PHP, MySql, Jquery, AngularJS, Ajax, Codeigniter, Laravel Tutorial

Tuesday, 4 February 2020

Instant Search with Pagination in PHP Mysql jQuery and Ajax

If you looking for tutorial on Ajax Live Data Search with Pagination in PHP Mysql using Ajax, then you have land on right page. Because in this post, we have make tutorial on Instant Mysql Data search with Pagination feature by using PHP script with Ajax and jQuery. If you are using PHP for you web development, then you have know Data Search and Pagination, both are required functionality of any web based application. So, you have to learn both things, because Search and Pagination both are used in most of the PHP based web application.

In Web development, Search is the most powerful functionality in the Mysql Database management section. And if Search functionality is live or instant, then you can quickly ge the filter data from the large amount of data. If you are User then you can get the relevant set of data from the large list of records in a seconds. For make live search feature, here we have use Ajax with PHP script. So, we can get the filter data on web page without refresh of web page. So, by using Ajax with PHP script we can easily implement Live search functionality for our PHP based web application.

If we have use Live search feature in our web application, then there is large amount of data has been filter from Mysql database, then on single web page we have load large filter data, then it will slow down our application. For prevent loading of large filter data on single web page, here we have use the Pagination functionality of web development. Here we will implement pagination without page refresh, because here also for Pagination we will use Ajax with PHP script. So we can easily implement Ajax pagination with PHP script. If we have use Ajax Pagination, then it will make the list of data more user friendly. In this post, we will implement Ajax Live Search with Pagination feature in PHP application. Below you can find the complement source of code this tutorial.

Source Code

index.php

         

Live Data Search with Pagination in PHP Mysql using Ajax


Dynamic Data

fetch.php

By follow Source code of Ajax Live Mysql Data Search with Pagination using PHP script, then you can definately build or PHP Ajax Live Search box for you web application and if you still want to get any assistance, then you can email us at webslesson@gmail.com.

Читайте также:  Минисайт

Источник

PHP Pagination Code with Search using AJAX

Fetching and displaying a bulk of records on a single fetch is not a good practice. It will increase the server load and has more disadvantages.

Pagination will resolve this problem.

Most of the website shows links of numbers for pagination. There are different ways to add pagination. Some of them are listed below.

  1. Previous-next navigation controls.
  2. Number links 1, 2, 3 … up to … total number of pages.
  3. A restricted number of page links that are expandable on navigation.

In this tutorial, we are going to see an example of a PHP pagination code with search sorting.

This example adds pagination to a list of records displayed in a tabular view. It uses jQuery AJAX to fetch records for each page from the database.

What is pagination

  • The Pagination is a simple method to split voluminous data into pages. It prevents the disadvantages of displaying all records on a page.
  • It shows a limited number of records per page which lets a quick glance at the page result.
  • Paginated results require the start offset and per-page count to set the LIMIT to fetch.

Merits and demerits of pagination

There are more merits of using PHP pagination. Some of them are listed below.

  • It increases efficiency by loading the page content quickly.
  • It reduces server overload.
  • The pagination links will reveal the approximate data stuff of the application.
  • It will greatly improve site responses on displaying the media library with pagination.
  • It increases user experience in browsing the paginated results.

Almost PHP pagination has no demerits, but it is not suitable in some scenarios. For example,

  • It is skippable for applications displaying records that are countable by fingers.
  • Without relational attributes (like prev, next URL), it gives negative impacts on SEO’s point of view.

About this example

This example has the code to group the pagination, sorting, search for a listing page.

The pagination UI will show limited pagination links. It extends the link stack on moving forward page by page.

In this example, we have three options based on which pagination format will be changed.

The pagination request carries over the search and sorting parameters if exist.

It calls an AJAX script to send the pagination, search and sort request data to the PHP.

It allows to search and sort by name via AJAX. The default sorting order is ascending (ASC) to sort the name column in alphabetical order.

Читайте также:  Css ссылка по центру

The on-click event of the column header switches the sorting orders between ‘ASC’ and ‘DESC’

File structure

This is the file structure of the PHP pagination example. The has the paginated results to be rendered into the home page.

The list.php is to display the results queried with a per-page limit. The perpage.php file contains the handlers to prepare the pagination HTML to be shown into the UI.

php pagination files

Home page HTML to render pagination UI

The following code shows the HTML to display the member database results.

It displays a name-wise search panel above the table header. Also, the first column displays the member names with a sorting feature.

The PHP pagination, search and sorting actions give results without page refresh. It uses jQuery AJAX to request PHP to perform these actions without reloading the page.

The “listViaAJAX()” JavaScript function request PHP pagination result via AJAX. The “sortList()” function, sets the ordering request parameters to get the sorted results.

The search form submit-button invokes the listViaAJAX() code to send the keyword. In the callback, it gets the response HTML from the server and updates the UI.

    

PHP Pagination with Search

Name Registered Date Subscription Amount

Paginated database results in a tabular view

This list.php file is to fetch and output the results to the UI. This PHP pagination request is called via AJAX by clicking the page links.

The below code prepares the MySQL query to fetch the data from the database. This query uses LIMIT to display the data with the per-page limit.

This code gets the total records’ count and per-page limit. It passes these two parameters the showPerpage() function to generate the PHP pagination HTML.

Added to the pagination parameters, this PHP code receives the search, sort request.

It prepares the WHERE and ORDER BY clause based on the search and sort request. These clauses are attached to the query to get the filtered result.

 $orderBy = "name"; $order = "asc"; if (! empty($_POST["order_type"])) < $order = $_POST["order_type"]; >$nextOrder = "asc"; if ($order == "asc") < $nextOrder = "desc"; >$query = "SELECT * from tbl_member " . $queryCondition . " ORDER BY " . $orderBy . " " . $order . " "; $href = 'index.php'; $perPage = 5; $page = 1; if (isset($_POST['pagination_offset'])) < $page = $_POST['pagination_offset']; >$start = ($page - 1) * $perPage; if ($start < 0) $start = 0; $queryWithLimit = $query . " limit " . $start . "," . $perPage; $resultpage = $dataSource->select($queryWithLimit, $paramType, $paramValue); if (! empty($resultpage)) < $count = $dataSource->getRecordCount($query, $paramType, $paramValue); $resultpage["perpage"] = showperpage($count, $perPage, $href); > ?> $v) < if (is_numeric($k)) < ?>    $ > > if (isset($resultpage["perpage"])) < ?>  ?> 

External handlers to prepare PHP pagination controls

It uses a constant to split the database results into pages. This code uses the following two functions.

  • perpage() – prepares the HTML for the pagination UI links.
  • showperpage() – returns pagination HTML response.

The perpage() function adds the onClick attribute to the PHP pagination links. It calls the AJAX code by sending the pagination query offset.

It uses the offset, per-page count and the total number of records to calculate the number of pages. It runs a loop through the number of pages to display the pagination links.

It adds HTML attributes to highlight the current page among the page links.

1) < if(($_POST["pagination_offset"]-3)>0) < if($_POST["pagination_offset"] == 1) $output = $output . '1'; else $output = $output . ''; > if(($_POST["pagination_offset"]-3)>1) < $output = $output . '. '; >for($i=($_POST["pagination_offset"]-2); $i$pages) break; if($_POST["pagination_offset"] == $i) $output = $output . ''.$i.''; else $output = $output . ''; > if(($pages-($_POST["pagination_offset"]+2))>1) < $output = $output . '. '; >if(($pages-($_POST["pagination_offset"]+2))>0) < if($_POST["pagination_offset"] == $pages) $output = $output . '' . ($pages) .''; else $output = $output . ''; > > return $output; > function showperpage($count, $per_page = 5, $href) < $perpage = perpage($count, $per_page,$href); return $perpage; >?> 

jQuery AJAX script to call paginated list from PHP

This jQuery AJAX script calls the PHP pagination code on the server-side.

It calls the PHP endpoint index.php by sending the form data. It serializes and posts the following data to the PHP.

  • Page offset. Default is 0.
  • Search keyword.
  • Sorting order. Default is ascending order(ASC).

The sortlist() function toggles the sorting order by clicking the corresponding column name.

function listViaAJAX() < $.ajax(< method: "POST", url: "list.php", data: $("#frmSearch").serialize(), >).done(function( response ) < $("#table-body").html(response); >); > function sortList() < if($("#order-type").val() == 'asc') < $("#order-type").val('desc'); $(".sortable img").attr("src", "images/sort-up.svg"); >else < $("#order-type").val('asc'); $(".sortable img").attr("src", "images/sort-down.svg"); >listViaAJAX(); > 

Database script

Download the below database script into your environment. It shows the database schema with the table structure.

It also has the sample data in the target database table. It supports to start seeing the PHP pagination code result with search.

-- -- Database: `blog_eg` -- -- -------------------------------------------------------- -- -- Table structure for table `tbl_member` -- CREATE TABLE `tbl_member` ( `id` int(255) NOT NULL, `name` varchar(256) NOT NULL, `date` date NOT NULL, `subscription_amount` varchar(256) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- -- Dumping data for table `tbl_member` -- INSERT INTO `tbl_member` (`id`, `name`, `date`, `subscription_amount`) VALUES (1, 'Jennifer d\' Sousa ', '2022-01-20', '10'), (2, 'Tom Adomian', '2021-12-25', '10'), (3, 'Vincent Thomas', '2021-12-24', '100'), (4, 'Lara Ricky', '2021-12-25', '100'), (5, 'Kevin Fernandes', '2021-12-17', '100'), (6, 'Leena Christy', '2021-12-16', '40'), (7, 'Christian Benchamin', '2021-12-19', '60'), (8, 'Abraham Maslow', '2021-12-17', '175'), (9, 'Helan Immaculate', '2021-12-16', '190'), (10, 'Jane Jancy', '2021-12-19', '30'); -- -- Indexes for dumped tables -- -- -- Indexes for table `tbl_member` -- ALTER TABLE `tbl_member` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `tbl_member` -- ALTER TABLE `tbl_member` MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=24; 

PHP pagination output

The following screenshot displays the PHP pagination code. It shows the tabular results with pagination links and a search panel.

Источник

Оцените статью