How to create Pagination with Codeigniter and MySql
In this tutorial, I will inform you How to create Pagination with 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.
I have share post next and previous button with code in this tutorial and I hope it will be helping you. below example in 3 records displayed per page.
1. Create a Register controller and put bellow code in this file.
1 2 3 4 5 6 7 8 9 10 11 12 | 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); } |
2. Create a new register view file and put bellow code in this file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | <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> |
3. After 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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); } |