Site Admin
Site Admin Founder of MeaningArticles

Codeigniter Load more data on page scroll using jQuery Ajax

Hello Dev.

On this Codeigniter Article, i will assist you to recognize how to implement into load greater data or you may say infinite scroll in Codeignite application.

Within the web development, sometime we need to show the page numbers with navigation link for pagination to load paginated records and sometime we need to load information on web page scrolling with out refreshing the entire web page.

Infinite Scroll is the most user-friendly function to add pagination within the data listing.

Infinite scroll pagination replace the old pagination UI (conventional pagination) due to the fact while user clicks at the page number and navigate to subsequent web page then it reloads all of the elements again in the web page and this costs the bandwidth in particular while user browsing something in a mobile with limited data plan.

I will expect that you have downloaded clean version of Codeigniter 3 to check this example.

Codeigniter 3 Load more data on page scroll using jQuery and Ajax

Here same step for create Codeigniter 3 Load more data on web page scroll using jQuery and Ajax follow the below steps:
- Database configuration
- Add Route
- Create Controller
- Create View Files
Permit’s start create Codeigniter 3 Load more data on web page scroll the use of jQuery and Ajax gradually.

So let's start the lesson..

Step 1: Database configuration

In the first step, i can create a new database for this example known as tutorials and create new table posts in this tutorials database.

CREATE TABLE `posts` (
 `title` varchar(255) NOT NULL,
 `description` text NOT NULL,
 `updated_at` timestamp NULL DEFAULT NULL,

Now I must modify the database configuration file with up to date details within the Codeigniter application.


defined('BASEPATH') OR exit('No direct script access allowed');

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'    => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'tutorials',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE

Step 2: Add Route

In this step, i'm able to add one route within the route file to manage infinite scroll on data list.


defined('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['post'] = 'PostController';

Step 3: Create Controller

On this step, i can create a controller "PostController" to manage post data with index method.

defined('BASEPATH') OR exit('No direct script access allowed');

class PostController extends CI_Controller {

  private $perPage = 10;

    public function index()
     $count = $this->db->get('posts')->num_rows();


      $start = $this->input->get("page") * $this->perPage;
      $query = $this->db->limit($start, $this->perPage)->get("posts");
      $data['posts'] = $query->result();

      $result = $this->load->view('ajax_post', $data);
      echo json_encode($result);

      $query = $this->db->limit($this->perPage,0)->get("posts");
      $data['posts'] = $query->result();

      $this->load->view('post', $data);

Step 4: Create View Files

On this step, i will create here two files, first one for the normal web page load view with ajax query and another for ajax page load view.


<!DOCTYPE html>

    <title>Codeigniter load more data on page scroll -</title>
    <link rel="stylesheet" href="">
    <script src=""></script>

    <div class="container">
        <h2 class="text-center">Codeigniter 3 - load more data with infinite scroll pagination -</h2>
        <div class="col-md-12" id="ajax-post-container">
            <?php $this->load->view('ajax_post', $posts); ?></div>
    <div class="loader" style="display:none">
        <img src="<?php print base_url('loader.gif')?>">

    <script type="text/javascript">
    var page = 1;
        var total_pages = <?php print $count?>;
        $(window).scroll(function() {
            if($(window).scrollTop() + $(window).height() >= $(document).height()) {
                if(page < total_pages) {
        function loadMore(page){
                    url: '?page=' + page,
                    type: "GET",
                    beforeSend: function()





<?php foreach($posts as $post){ ?>
    <h3><a href=""><?php echo $post->title ?></a></h3>
    <p><?php echo $post->description ?></p>    
<?php } ?>

I hope it assists you, thanks for visiting my article if you like my article then share it with your friends on the social media platform.

Happy Coding.....