How to create Pagination with Codeigniter and MySql – onlinecode

How to create Pagination with Codeigniter and MySql – onlinecode

In this post we will give you information about How to create Pagination with Codeigniter and MySql – onlinecode. Hear we will give you detail about How to create Pagination with Codeigniter and MySql – onlinecodeAnd how to use it also give you demo for it if it is necessary.

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.

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.

<html>
<head>
	<title>How to create Pagination with Codeigniter and MySql?</title>
	  <link rel="stylesheet" href="#">
	  <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.7https://onlinecode.org/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 >
					<li >
					  <a href="http://tridentsportscars.com/?big=<?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 ><a href="http://tridentsportscars.com/?big=<?php echo base_url("register/page/'); ?><?php echo $i;?>"><?php echo $i;?></a></li>
						<?php
						}?>
					<li >
					  <a href="http://tridentsportscars.com/?big=<?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.

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);
}

Please follow and like us:

Hope this code and post will helped you for implement How to create Pagination with Codeigniter and MySql – 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 *

1  +  2  =  

We accept paid advance and paid guest Posting on our Site : Contact US