Codeigniter Ajax Pagination


Codeigniter Ajax Pagination

Codeigniter Ajax Pagination today solve this task so just share with your. Today we will discuss how to create ajax pagination in Codeigniter Framework. Codeigniter have the pagination library by default.  But many times we are needed to implemented ajax based pagination in codeigniter. and pagination responsive design you implemented your own designed.

Already we discussed codeigniter framework and other related topics so just go the task at first we need to add ajax pagination into the Codeigniter Pagination library.  Copy the Codeigniter pagination library and modify with ajax pagination code.  Rename the Pagination Class to Ajaxpagination and insert the Ajaxpagination.php file into the library folder.  you can download the CodeIgniter Ajax pagination library from the Click Here

   
Codeigniter Ajax Pagination
Codeigniter Ajax Pagination
First Create table


CREATE TABLE `dent_adminusers` (
  `id` int(11) NOT NULL auto_increment,
  `adminname` varchar(50) NOT NULL,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `status` enum('Active','Suspend','Deactive') NOT NULL default 'Deactive',
  `mailstatus` enum('0','1') NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

Controller

Create a controller file named posts.php with Posts classed. Into the __construct() function we need to load post model.  Ajaxpagination library. And we set the per page data limit into the $this-perPage variable.


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Posts Management class created by CodexWorld
 */
class Posts extends CI_Controller {
    
    function __construct() {
        parent::__construct();
        $this->load->model('post');
        $this->load->library('Ajaxpagination');
        $this->perPage = 1;
    }
    
    public function index()
    {
        $data = array();
        
        //total rows count
        $totalRec = count($this->post->getRows());
        
        //pagination configuration
        $config['first_link']  = 'First';
        $config['div']         = 'postList'; //parent div tag id
        $config['base_url']    = base_url().'posts/ajaxPaginationData';
        $config['total_rows']  = $totalRec;
        $config['per_page']    = $this->perPage;
        
        $this->ajax_pagination->initialize($config);
        
        //get the posts data
        $data['posts'] = $this->post->getRows(array('limit'=>$this->perPage));
        
        //load the view
        $this->load->view('posts/index', $data);
    }
    
    function ajaxPaginationData()
    {
        $page = $this->input->post('page');
        if(!$page){
            $offset = 0;
        }else{
            $offset = $page;
        }
        
        //total rows count
        $totalRec = count($this->post->getRows());
        
        //pagination configuration
        $config['first_link']  = 'First';
        $config['div']         = 'postList'; //parent div tag id
        $config['base_url']    = base_url().'posts/ajaxPaginationData';
        $config['total_rows']  = $totalRec;
        $config['per_page']    = $this->perPage;
        
        $this->ajax_pagination->initialize($config);
        
        //get the posts data
        $data['posts'] = $this->post->getRows(array('start'=>$offset,'limit'=>$this->perPage));
        
        //load the view
        $this->load->view('posts/ajax-pagination-data', $data, false);
    }
}


Model:

Codeigniter Ajax Pagination Create a Model file follows Code.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Post extends CI_Model{
    
    function __construct() {
        $this->postTable = 'posts';
    }
    
    function getRows($params = array())
    {
        $this->db->select('*');
        $this->db->from($this->postTable);
        $this->db->order_by('created','desc');
        
        if(array_key_exists("start",$params) && array_key_exists("limit",$params)){
            $this->db->limit($params['limit'],$params['start']);
        }elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){
            $this->db->limit($params['limit']);
        }
        
        $query = $this->db->get();
        
        return ($query->num_rows() > 0)?$query->result_array():FALSE;
    }
}
?>


View

Codeigniter Ajax Pagination responsive or your design modify view page.


<h1>Posts</h1>
<div id="container">
    <ul class="list" id="postList">
        <?php if(!empty($posts)): foreach($posts as $post): ?>
        <li>
            <p><b>Title:</b>&nbsp;<?php echo $post['title']?></p>
            <p><b>Content:</b>&nbsp;<?php echo $post['content']?></p>
            <p><b>Created:</b>&nbsp;<?php echo $post['created']?></p>
        </li>
        <?php endforeach; else: ?>
        <li class="err_msg">Post(s) not available.</li>
        <?php endif; ?>
        <?php echo $this->ajax_pagination->create_links(); ?>
    </ul>
</div>

Codeigniter Ajax Pagination Dev2Tricks 5 of 5
Codeigniter Ajax Pagination Codeigniter Ajax Pagination today solve this task so just share with your. Today we will discuss how to creat...

Share this

Related Posts

Previous
Next Post »