CodeIgniter pagination

2019-12-06 Raj Thakur

In this tutorial, We are implementing pagination in Codeigniter with the database. If your database is too large then the SELECT query may return result into thousand and millions of records or row. It's possible to display all these records on the same page but in some cases, slow internet speed has not loaded all record properly that reason we can split this result into multiple pages. Pagination is nothing but paging means all result in multiple pages instead of showing them all on one page. it makes that page so long and would take so much time to load. In the Codeigniter, we need configuration for the page division like URL of the page, total rows and per page record. also, Codeigniter provides you with including your begin for pagination layouts. for more information please visit codeigniter.com

Controller

public function index()
	{	
		$config=[
		'base_url'=>base_url('Home/index'),
		'total_rows' => $this->Data_retrieval_without_login->all_post_data_total_row(),
		'per_page' => 30,
		];
		$config['first_tag_open'] = '<li class="page-item page-link">';
        $config['first_tag_close'] = '</li>';
		 $config['num_tag_open'] = '<li class="page-item page-link">';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="page-item active">
    	  <a class="page-link" ><span class="sr-only">(current)</span>';
        $config['cur_tag_close'] = '</a></li>';
        $config['next_link'] = '  <li class="page-item page-link"> Older &rarr;
            </li>';
        $config['prev_link'] = '<li class="page-item page-link">&larr; Newer
            </li>';   
        $config['last_tag_open'] = '<li class="page-item page-link">';
        $config['last_tag_close'] = '</li>';
		// Initialize
		$this->pagination->initialize($config);
		$all_post=$this->Data_retrieval_without_login->all_post_data($config['per_page'],$this->uri->segment(3));
		$this->load->view('blog/index',['all_posts'=>$all_post]);

	}

Views

 <nav aria-label="Page navigation">
     <ul class="pagination justify-content-start">
        <?php echo $this->pagination->create_links(); ?>  
     </ul>
 </nav>

About author

Card image cap
Raj Thakur

I’m a full-stack developer, with a strong programming knowledge on web base frameworks.!

-full-stack developer

0 Comments


Scroll to Top