Infinite scroll pagination using codeigniter jquery and ajax – onlinecode

Infinite scroll pagination using codeigniter jquery and ajax – onlinecode

In this post we will give you information about Infinite scroll pagination using codeigniter jquery and ajax – onlinecode. Hear we will give you detail about Infinite scroll pagination using codeigniter jquery and ajax – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this tutorial, we are going to learn how to create infinite scroll pagination using Codeigniter jquery and ajax. we have seen on Facebook, Twitter or other websites on that automatically loaded content when scrolling down to the web page.

So you can follow the below steps and see our example for mouse scroll pagination in Codeigniter.

Overview

Create a Database and Table

Connect to Database

Get the limit data

Get the infinite data

Create a Database and Table

First of all for we will create a database and table. after then we will add dummy data in the table. here we take the products table and also added products related data into the product table. so you can see the below code.

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'codeigniter_pagination',
	'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
);

Connect to Database

Here in this step, we will open database.php file in the config directory and some changes in this file like as hostname, database username, database password, and database name.

Get the limit data

Here in this step, we have to need limited data for display data. so we are displaying 10 data per rows. for that, we will take a constant variable so we can display 10 data per rows. now here we will take two hidden inputs fields for the current row position and the total number of row

So you can see our index method code for the first time get rows records.

 public function index() {
        $row= 0;
        $row_per_page = 10;
        $all_product_count = $this->Product_model->all_product_count();
        //echo $all_product_count; die;
        $data_proudct = $this->Product_model->get_product($row,$row_per_page);
        $data['products'] =$data_proudct;
        $data['all_product_count'] =$all_product_count;
        $this->load->view('product', $data);
    }

Get the infinite data

Here in this step, we have used some functions. you can see it.

$(window).scrollTop() function when scroll then it will return current vertical position.

$(document).height() function return height of the document.

$(window).height() function return pixel value of the height of the (browser) window.

When we scroll down if the current position and bottom position are the same then it will be passed next current row data using ajax and call into the pagination method. after it will be helpful for getting limit data into the query.

    public function pagination(){
        $row = $_POST['row'];
        $row_per_page = 10;
        $data_proudct = $this->Product_model->get_product($row,$row_per_page);

        $html ='';
        foreach ($data_proudct as $product){

                $html .= '<tr  id="product_'.$product->id.'">';
                $html .= '<td>'.$product->title.'</td>';
                $html .= '<td>'.$product->description.'</td>';
                $html .= '<td>'.$product->price.'</td>';
                $html .= '<td>'.$product->created_at.'</td>';
                $html .= '</tr>';
        }
        echo $html;
    }

 

so you can see our full code.

views/product.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Infinite scroll pagination using codeigniter jquery and ajax - Web Programming Tutorials</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <script type="text/javascript">
         $(document).ready(function(){

            $(window).scroll(function(){

                var position = $(window).scrollTop();
                var bottom = $(document).height() - $(window).height();

                if( position == bottom ){
                    var row = Number($('#row').val());
                    var allcount = Number($('#all_product_count').val());
                    var rowperpage = 10;
                    row = row + rowperpage;

                    if(row <= allcount){
                        $('.ajax-load').show();
                        var url = "<?php echo base_url(); ?>index.php/welcome/pagination";
                        $('#row').val(row);
                        $.ajax({
                            url: url,
                            type: 'post',
                            data: {row:row},
                            success: function(response){
                                $('.ajax-load').hide();
                                $(".product:last").after(response).show().fadeIn("slow");
                            }
                        });
                    }
                    else
                    {
                        $('#remove').remove();
                        $(".product:last").after('<tr id="remove"><td colspan="4" style="text-align: center;"><b>No Data Available</b></td></tr>');

                    }
                }
            });
        });
    </script>
    <style>
        table {
            border-collapse: collapse;
            border-spacing: 0;
            width: 100%;
            border: 1px solid #ddd;
        }

        th,td {
            text-align: left;
            padding: 8px;
        }

        tr:nth-child(even){background-color: #f2f2f2}
    </style>
</head>
<body>

<div >
    <h2>Product Table</h2>
    <div style="overflow-x:auto;">
        <table >
            <thead>
            <tr>
                <th>Product Name</th>
                <th>Description</th>
                <th>Price</th>
                <th>Create</th>
            </tr>
            </thead>
            <tbody>
            <?php
            foreach ($products as $product){
            ?>
            <tr  id="product_<?php echo $product->id; ?>">
                <td><?php echo $product->title; ?></td>
                <td><?php echo $product->description; ?></td>
                <td><?php echo $product->price; ?></td>
                <td><?php echo $product->created_at; ?></td>
            </tr>
            <?php } ?>
            </tbody>
        </table>
        <input type="hidden" id="row" value="0">
        <input type="hidden" id="all_product_count" value="<?php echo $all_product_count; ?>">
    </div>
    <div  style="display: none;">
        <div  style="text-align: center;"><img width="100" height="100" src="https://www.primebldg.com/wp-content/uploads/2017/09/ajax-loader.gif"></div>
    </div>
</div>
</body>
</html>

controllers/Welcome.php

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

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		
	 *	- or -
	 * 		
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	
	
	function __construct() {
        parent::__construct();
        $this->load->model("Product_model");
    }

    public function index() {
        $row= 0;
        $row_per_page = 10;
        $all_product_count = $this->Product_model->all_product_count();
        //echo $all_product_count; die;
        $data_proudct = $this->Product_model->get_product($row,$row_per_page);
        $data['products'] =$data_proudct;
        $data['all_product_count'] =$all_product_count;
        $this->load->view('product', $data);
    }

    public function pagination(){
        $row = $_POST['row'];
        $row_per_page = 10;
        $data_proudct = $this->Product_model->get_product($row,$row_per_page);

        $html ='';
        foreach ($data_proudct as $product){

                $html .= '<tr  id="product_'.$product->id.'">';
                $html .= '<td>'.$product->title.'</td>';
                $html .= '<td>'.$product->description.'</td>';
                $html .= '<td>'.$product->price.'</td>';
                $html .= '<td>'.$product->created_at.'</td>';
                $html .= '</tr>';
        }
        echo $html;

    }
}
?>

models/Product_model.php

<?php

class Product_model extends CI_Model {

    public function all_product_count(){
		
		$query = $this->db->query("SELECT * FROM products")->result();
        return count($query);
    }

    public function get_product($page,$row_per_page)
    {
        $query = $this->db->query("SELECT * FROM products ORDER BY id desc limit ".$page.",".$row_per_page);
        return $query->result();
    }

}
?>

We have suggested if you still do not read our article related article then you can read our article related article. How to create Pagination with PHP and MySql.

Please follow and like us:

Hope this code and post will helped you for implement Infinite scroll pagination using codeigniter jquery and ajax – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

8  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US