In this tutorial, I will inform you How to create Pagination in Codeigniter and MySql.
Pagination means document content dividing into multiple pages. whenever we have one long page at that time we dividing into multiple pages.
if you want to create pagination using MySQL at that time use limit clause, it takes two arguments first is “offset” and the second is the “end”. end means a number of records returned from the database.
here we not use of codeigniter pagination library just we use the custom create pagination in codeigniter. so you can see below steps
Create Register controller
public function index() { $total=3; $start=0; $arrData['active_page']=1; $query = $this->db->query("SELECT * FROM register LIMIT $start,$total"); $data = $query->result_array(); $arrData['register_detail'] = $data; $arrData['total'] = $total; $this->load->view('register',$arrData); }
Create view file
<html> <head> <title>How to create Pagination with Codeigniter and MySql?</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script </head> <body> <table border="1" align="center"> <tr> <td>First Name</td> <td>Last Name</td> <td>Email</td> <td>Mobile</td> </tr> <?php foreach($register_detail as $rg){ ?> <tr> <td><?php echo $rg['first_name']; ?></td> <td><?php echo $rg['last_name']; ?></td> <td><?php echo $rg['email']; ?></td> <td><?php echo $rg['mobile']; ?></td> </tr> <?php } ?> <tr> <td colspan="4"> <ul class="pagination"> <li class="<?php if($active_page==1){ echo 'disabled';} ?>"> <a href="<?php if($active_page==1){ echo '#';} else {?><?php echo base_url('register/page'); ?>/<?php echo $active_page-1; }?>" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <?php $query = $this->db->query("SELECT * FROM register"); $data = $query->result_array(); $total1 = count($data); $total_pages = ceil($total1 / $total); for($i=1;$i<=$total_pages;$i++) {?> <li class="<?php if($active_page==$i){ echo 'active'; } ?>"><a href="<?php echo base_url('register/page/'); ?><?php echo $i;?>"><?php echo $i;?></a></li> <?php }?> <li class="<?php if($active_page==$total_pages){ echo 'disabled';} ?>"> <a href="<?php if($active_page==$total_pages){ echo '#';} else {?><?php echo base_url('register/page/'); ?><?php echo $active_page+1; }?>" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </td> </tr> </table> </body> </html>
Create new page method in Register controller
When perticular page on click then this bellow method call, it method page wise get data from the database and redirect in the view file.
public function page($page) { $total=3; $active_page=$page; if (isset($page)) { $page = $page; } else { $page=1; } $start = ($page-1) * $total; $query = $this->db->query("SELECT * FROM register LIMIT $start,$total"); $data = $query->result_array(); $arrData['active_page']=$active_page; $arrData['register_detail'] = $data; $arrData['total'] = $total; $this->load->view('register',$arrData); }