Example of PHP Code Demonstrating Pagination of Database Records

The offset indicates the desire to retrieve a specified number of results beginning from the 11th result. By employing this method, you can easily utilize it on the results page. This approach is effective for non-ajax requests. Additionally, you have the option to execute the following step: Process.php.


Solution:

As stated in my comments.

You could do the following:

$(document).ready(function()
{
    $('.paginate').live('click', function(e)
    {
        e.preventDefault();
        var btnPage = $(this);
        $.ajax(
        {
            url : btnPage.attr('href'),
            success : function(resp)
            {
                // replace current results with new results.
                $('#project_section').html(resp);
            },
            error : function()
            {
                window.location.href = btnPage.attr('href');
            }
        });
    });
});

The aforementioned action will simulate the act of clicking on every pagination link.

My recommendation is to separate the PHP code and HTML responsible for generating the “results” list into a distinct file.

On the results page, you can easily utilize

include('path-to-results-file.php');

for non-ajax requests, followed by:


Process.php

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
    include('path-to-results-file.php');
    die();
}

The mentioned code will identify if there has been an ajax request made. In such cases, rather than displaying the entire page along with the results, it will only display the results and the pagination.

Revised to incorporate a more comprehensive clarification.

Here is an extremely basic illustration of my point.

The existing process.php file.

    

...

    
    

An updated version of process.php has been released.



...

    
    
    


Updated results-file.php path


When accessing

process.php

through your browser, whether with or without Javascript enabled, it will function in the same manner.

Upon visiting

process.php

, if you click on any pagination links with JavaScript enabled,

process.php

will recognize the use of Ajax and solely return the results table.

Frequently Asked Questions

Posted in Php