PHP Codeigniter - Load more data on page scroll using jQuery and Ajax

PHP Codeigniter - Load more data on page scroll using jQuery and Ajax

In this PHP Codeigniter tutorial, I will let you know how to implement load more feature or you can say infinite scroll in Codeignite app.

In 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 data on page scrolling without refreshing the whole page.

Infinite Scroll is the most user-friendly feature to add pagination in the data list.

Infinite scroll pagination replace the old pagination UI (conventional pagination) because when user clicks on the page number and navigate to next page then it reloads all the elements again in the page and this costs the bandwidth mainly when user browsing something in a mobile with limited data plan.

I will assume that you have downloaded fresh version of Codeigniter 3 to test this example.

Step 1: Database configuration

In the first step, I will create a new database for this example called 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 have to modify the database configuration file with updated details in 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 will add one route in 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

In this step, I will 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

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

<!DOCTYPE html>
	<title>PHP Codeigniter load more data on page scroll</title>
	<link rel="stylesheet" href="">
	<script src=""></script>

<div class="container">
	<h2 class="text-center">PHP Codeigniter 3 - load more data with infinite scroll pagination</h2>
	<div class="col-md-12" id="ajax-post-container">
		  $this->load->view('ajax_post', $posts);

<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 } ?>

Phone: (+91) 8800417876
Noida, 201301
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort