Friday, 27 May 2016

Pagination Using PHP

<?php
$conn=mysqli_connect("localhost","root","","pipo");


$perpage = 5;

if(isset($_GET["page"])){
    $page = intval($_GET["page"]);
}
else {
$page = 1;
}
$calc = $perpage * $page;
$start = $calc - $perpage;
$result = mysqli_query($conn, "select * from dept where city='hosur' Limit $start, $perpage ");

//$rows = mysqli_num_rows($result);

//if($rows){
    //$i = 0;
    while($row = mysqli_fetch_assoc($result)) {
?>
<table style="border: 1px #000000 solid;" width="400" cellspacing="2" cellpadding="2" align="center">

<tbody>
<tr style="background-color: #cccccc;">

<td style="font-weight: bold;font-family: arial;"><?php echo $row["Name"]; ?></td>

</tr>

<tr>

<td style="font-family: arial;padding-left: 20px;"><?php echo $row["job"]; ?></td>

</tr>
<?php
}
//}
?>

</tbody>
</table>

<table width="400" cellspacing="2" cellpadding="2" align="center">
<tbody>
<tr>
<td align="center">

<?php

if(isset($page)){
$result = mysqli_query($conn,"select count(*) As Total from dept where city='hosur' ");
//$rows = mysqli_num_rows($result);
//if($rows){
$rs = mysqli_fetch_assoc($result);
$total = $rs["Total"];

//}
$totalPages = ceil($total / $perpage);
if($page <=1 ){
echo "<span id='page_links' style='font-weight: bold;'>Prev</span>";
}
else{
$j = $page - 1;
echo "<span><a id='page_a_link' href='pagination.php?page=$j'>< Prev</a></span>";
}
for($i=1; $i <= $totalPages; $i++){
if($i<>$page){
echo "<span><a id='page_a_link' href='pagination.php?page=$i'>$i</a></span>";
}
else{
echo "<span id='page_links' style='font-weight: bold;'>$i</span>";
}
}
if($page == $totalPages ){
echo "<span id='page_links' style='font-weight: bold;'>Next ></span>";
}
else{
$j = $page + 1;
echo "<span><a id='page_a_link' href='pagination.php?page=$j'>Next</a></span>";
}
}
?></td>
<td></td>
</tr>
</tbody>
</table>


</body>
</html>

No comments:

Post a Comment