For doing pagination We Sholud know that how many number of records are exits in Table.Then We can decide that how many records display on single page.(Record Per Page).Then we should know that how many needed for display total records.For that we divide Total number of records to Record per page, and we get the total pages to display total records.
CODE:
<?php
include 'config.php';
$select=mysql_query("SELECT id FROM `pagination`");
$count=mysql_num_rows($select);
$page_limit=3;
$total_pages=ceil($count/$page_limit);
?>
<html>
<head>
<title>Pagination-PHP MySql</title>
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/bootstrap-responsive.css" rel="stylesheet">
</head>
<body>
<center><h1><a href="http://wayto-php.blogspot.in/" target="new">Way To PHP</a></h1></center>
<table class="table table-bordered" style="margin-top:100px;width:80%;margin-left:10%;">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Country</th>
<th>Occupation</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_GET['page']))
{
$page=$_GET['page'];
}
else
{
$page=1;
}
$start = ($page-1) * $page_limit;
$sql = "SELECT * FROM `pagination` LIMIT $start, $page_limit";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['country']; ?></td>
<td><?php echo $row['occupation']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<ul style="margin-left:45%;" class="pagination">
<?php for ($i=1; $i<=$total_pages; $i++) { ?>
<li style="display:inline; margin-left:8px; font-size:larger;"><a href="index.php?page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
<?php } ?>
</ul>
</body>
</html>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap.js"></script>
Incredible Code! I applied it to a meta task of mine and it had exactly the intended effect. Rather than ID I put a Lot Number (unsigned int special) as essential and it worked. Bluehost hosting plans
ReplyDelete