Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8203021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:23:54+00:00 2026-06-07T07:23:54+00:00

I have a table loaded via AJAX and along with the table the pagination

  • 0

I have a table loaded via AJAX and along with the table the pagination is also loaded through AJAX. The table contains a list of all users on my site limited to 30 at a time.

This is how I’m returning the response to the JavaScript from the controller:

    $users = $this->users_m->get_users($type, $offset);
    $num_rows = $this->users_m->user_stats($type);

    $config['per_page'] = 30;
    $config['num_links'] = 5;
    $config['total_rows'] = $num_rows[0];
    $this->pagination->initialize($config);

    echo json_encode(array(
        'users' => $users,
        'pagination' => $this->pagination->create_links()
    ));

All is well except the pagination is never correct. The first time it is but on subsequent requests it is not.

When using the pagination class in a non AJAX page, the page number I click becomes the active one. Here page 1 is always active (surrounded by <strong> tags as opposed to being a link). Secondly the numbers never change. I get:

[1] [2] [3] [4] [5] [6] [>] [Last >]

every time. Even if I click last I get the same numbers back, it doesn’t change.

How to get the pagination class to work with AJAX?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:23:57+00:00Added an answer on June 7, 2026 at 7:23 am

    Ok I came up with a working solution that I will post here in case anyone else has the same issue.

    I used the pagination class found here:

    http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/
    https://github.com/catchmyfame/PHP-Pagination-Class/blob/master/paginator.class.php

    but modified it to work both as a CI library and also with my particular javascript. Create a new file called

    Pagination_ajax.php

    and put it in the same location as the default pagination class in

    /system/libraries.

    This is the modified class:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    /*
     * PHP Pagination Class
     * @author admin@catchmyfame.com - http://www.catchmyfame.com
     * @version 2.0.0
     * @date October 18, 2011
     * @copyright (c) admin@catchmyfame.com (www.catchmyfame.com)
     * @license CC Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) - http://creativecommons.org/licenses/by-sa/3.0/
     */
    
    class Pagination_ajax {
        
        var $items_per_page;
        var $items_total;
        var $current_page;
        var $num_pages;
        var $mid_range;
        var $low;
        var $limit;
        var $return;
        var $default_ipp;
        var $querystring;
        var $ipp_array;
    
        function Paginator()
        {
            $this->current_page = 1;
            $this->mid_range = 7;
            $this->ipp_array = array(10,25,50,100,'All');
            $this->items_per_page = (!empty($_GET['ipp'])) ? $_GET['ipp']:$this->default_ipp;
        }
    
        function paginate()
        {
            if(!isset($this->default_ipp)) $this->default_ipp=25;
            if($_GET['ipp'] == 'All')
            {
                $this->num_pages = 1;
    //          $this->items_per_page = $this->default_ipp;
            }
            else
            {
                if(!is_numeric($this->items_per_page) OR $this->items_per_page <= 0) $this->items_per_page = $this->default_ipp;
                $this->num_pages = ceil($this->items_total/$this->items_per_page);
            }
            $this->current_page = (isset($_GET['page'])) ? (int) $_GET['page'] : 1 ; // must be numeric > 0
            $prev_page = $this->current_page-1;
            $next_page = $this->current_page+1;
            if($_GET)
            {
                $args = explode("&",$_SERVER['QUERY_STRING']);
                foreach($args as $arg)
                {
                    $keyval = explode("=",$arg);
                    if($keyval[0] != "page" And $keyval[0] != "ipp") $this->querystring .= "&" . $arg;
                }
            }
    
            if($_POST)
            {
                foreach($_POST as $key=>$val)
                {
                    if($key != "page" And $key != "ipp") $this->querystring .= "&$key=$val";
                }
            }
            if($this->num_pages > 10)
            {
                $this->return = ($this->current_page > 1 And $this->items_total >= 10) ? "<a data-page=\"$prev_page\" class=\"paginate\" href=\"#\">&laquo; Previous</a> ":"<span class=\"inactive\" href=\"#\">&laquo; Previous</span> ";
    
                $this->start_range = $this->current_page - floor($this->mid_range/2);
                $this->end_range = $this->current_page + floor($this->mid_range/2);
    
                if($this->start_range <= 0)
                {
                    $this->end_range += abs($this->start_range)+1;
                    $this->start_range = 1;
                }
                if($this->end_range > $this->num_pages)
                {
                    $this->start_range -= $this->end_range-$this->num_pages;
                    $this->end_range = $this->num_pages;
                }
                $this->range = range($this->start_range,$this->end_range);
    
                for($i=1;$i<=$this->num_pages;$i++)
                {
                    if($this->range[0] > 2 And $i == $this->range[0]) $this->return .= " ... ";
                    // loop through all pages. if first, last, or in range, display
                    if($i==1 Or $i==$this->num_pages Or in_array($i,$this->range))
                    {
                        $this->return .= ($i == $this->current_page And $_GET['page'] != 'All') ? "<a class=\"current\" href=\"#\">$i</a> ":"<a data-page=\"$i\" class=\"paginate\" href=\"#\">$i</a> ";
                    }
                    if($this->range[$this->mid_range-1] < $this->num_pages-1 And $i == $this->range[$this->mid_range-1]) $this->return .= " ... ";
                }
                $this->return .= (($this->current_page < $this->num_pages And $this->items_total >= 10) And ($_GET['page'] != 'All') And $this->current_page > 0) ? "<a data-page=\"$next_page\" class=\"paginate\" href=\"#\">Next &raquo;</a>\n":"<span class=\"inactive\" href=\"#\">&raquo; Next</span>\n";
                $this->return .= ($_GET['page'] == 'All') ? "<a class=\"current\" style=\"margin-left:10px\" href=\"#\">All</a> \n":"<a data-page=\"1\" data-all=\"true\" class=\"paginate\" style=\"margin-left:10px\" href=\"#\">All</a> \n";
            }
            else
            {
                for($i=1;$i<=$this->num_pages;$i++)
                {
                    $this->return .= ($i == $this->current_page) ? "<a class=\"current\" href=\"#\">$i</a> ":"<a data-page=\"$i\" class=\"paginate\" href=\"#\">$i</a> ";
                }
                $this->return .= "<a data-page=\"1\" data-all=\"true\" class=\"paginate\" href=\"#\">All</a> \n";
            }
            $this->low = ($this->current_page <= 0) ? 0:($this->current_page-1) * $this->items_per_page;
            if($this->current_page <= 0) $this->items_per_page = 0;
            $this->limit = ($_GET['ipp'] == 'All') ? "":" LIMIT $this->low,$this->items_per_page";
        }
        function display_items_per_page()
        {
            $items = '';
            if(!isset($_GET[ipp])) $this->items_per_page = $this->default_ipp;
            foreach($this->ipp_array as $ipp_opt) $items .= ($ipp_opt == $this->items_per_page) ? "<option selected value=\"$ipp_opt\">$ipp_opt</option>\n":"<option value=\"$ipp_opt\">$ipp_opt</option>\n";
            return "<span class=\"paginate\">Items per page:</span><select class=\"paginate\" onchange=\"window.location='$_SERVER[PHP_SELF]?page=1&ipp='+this[this.selectedIndex].value+'$this->querystring';return false\">$items</select>\n";
        }
        function display_jump_menu()
        {
            for($i=1;$i<=$this->num_pages;$i++)
            {
                $option .= ($i==$this->current_page) ? "<option value=\"$i\" selected>$i</option>\n":"<option value=\"$i\">$i</option>\n";
            }
            return "<span class=\"paginate\">Page:</span><select class=\"paginate\" onchange=\"window.location='$_SERVER[PHP_SELF]?page='+this[this.selectedIndex].value+'&ipp=$this->items_per_page$this->querystring';return false\">$option</select>\n";
        }
        function display_pages()
        {
            return $this->return;
        }
    }
    

    You can do a diff between this and the original download to see exactly what I changed.

    Controller code:

        public function get_users() {
    
            // pagination
            $this->load->library('pagination_ajax');
            $pages = new Pagination_ajax;
            $num_rows = $this->users_m->user_stats(); // this is the COUNT(*) query that gets the total record count from the table you are querying
            $pages->items_total = $num_rows[0];
            $pages->mid_range = 10; // number of links you want to show in the pagination before the "..."
            $pages->paginate();
    
            $users = $this->users_m->get_users($pages->limit); // your query
    
            echo json_encode(array(
                'users' => $users,
                'pagination' => $pages->display_pages()
            ));
    
        }
    

    Model Code:

    public function get_users($limit) {
    
        $sql = "SELECT *
                FROM `users`
                $limit";
    
        $query = $this->db->query($sql);
    
        $users = array();
        foreach ($query->result() as $row) {
            $users[] = array(
                'user_id'   => $row->user_id,
                'username'  => $row->username,
                'email'     => $row->email
            );
        }
    
        return $users;
    }
    

    JQuery:

    // pagination
    $('#pagination a').live('click', function() {
        var $this = $(this);
        var page = $this.data('page');
        var ipp = ($this.data('all')) ? 'All' : 30; // I am returning 30 results per page, change to what you want
    
        $.ajax({
            url: '/admin/users/get_users?page=' + page + '&ipp=' + ipp,
            dataType: 'json',
            success: function(response) {
    
                for(var i=0; i<response.users.length; i++) {
                    var user = response.users[i];
                    var tr = '<tr>' +
                                '<td>' + user.user_id + '</td>' +
                                '<td>' + user.username + '</td>' +
                                '<td>' + user.email + '</td>' +
                            '</tr>';
    
                    $('table tbody').append(tr);
                }
    
                // pagination
                $('#pagination').html(response.pagination);             
            },
            error: function() {
                alert('An error occurred');
            }
        });
    
        return false;
    });
    

    HTML

        <h1>Users</h1>
    
        <table>
            <thead>
                <th>ID</th>
                <th>Username</th>
                <th>Email</th>
            </thead>
            <tbody></tbody>
        </table>                        
        
        <div id="pagination"></div>
    

    CSS

    #pagination { overflow: hidden; margin-bottom: 10px; text-align: center; }
    #pagination a { display: inline-block; padding: 3px 5px; font-size: 14px; color: #333; border-radius: 3px; text-shadow: 0 0 1px #fff;  border: 1px solid #ccc;
    
        background: #ffffff;
        background: -moz-linear-gradient(top,  #ffffff 0%, #f6f6f6 47%, #ededed 100%);
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(47%,#f6f6f6), color-stop(100%,#ededed));
        background: -webkit-linear-gradient(top,  #ffffff 0%,#f6f6f6 47%,#ededed 100%);
        background: -o-linear-gradient(top,  #ffffff 0%,#f6f6f6 47%,#ededed 100%);
        background: -ms-linear-gradient(top,  #ffffff 0%,#f6f6f6 47%,#ededed 100%);
        background: linear-gradient(to bottom,  #ffffff 0%,#f6f6f6 47%,#ededed 100%);
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ededed',GradientType=0 );
    }
    #pagination a:hover { border: 1px solid #333; }
    #pagination a.current { color: #f00; }
    

    Hope someone finds this useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data which are loaded to table via jquery/ajax. Table has a
I have a set of images that are loaded via jQuery AJAX. For some
I have a collection of checkboxes loaded from a table that are loaded in
I have loaded my database with one table under Data Connections in the Server
I have 2 sections within a table view and I have loaded one section
I am currently loading HTMl content via AJAX. I have code for things on
In my program I have a table that, when loaded, has jQuery add some
In a jquery ui dialog i'm loading table of images via ajax. Once content
I am creating a page what will be loaded into another page via ajax.
I have a dataTable. The data of the dataTable is filled via ajax. A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.