Divide Content on multiple pages in PHP : Paging in PHP
Some time we have a lot of data like 600000 records and we want to show them but not all at a time so we use paging to divide them on separate pages in this post i will define how to use paging in PHP.
First we have to create a Database.
Database Name:Paging
Table:
CREATE TABLE `record` (
`Id` int(9) NOT NULL,
`Name` varchar(100) NOT NULL,
`Mobno` int(18) NOT NULL,
`Address` varchar(100) NOT NULL
) ;
And We put Dummy data on 100 records in it .
Now Retrieve that data.i use limit property in my query like
SELECT id,name,mobno,address from record limit 0,6
it will limit data in only 6 rows.
Now by using String Query's i create a hyper links containing page no and then on page load get them and use in limit value .
Formula: ( page_no - 1 ) * 6
Query String: <a href='usman.php?page=".$s1."'>".$s1."</a>"
Get Request:if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
Total Records:
CSS Code For Style:
First we have to create a Database.
Database Name:Paging
Table:
CREATE TABLE `record` (
`Id` int(9) NOT NULL,
`Name` varchar(100) NOT NULL,
`Mobno` int(18) NOT NULL,
`Address` varchar(100) NOT NULL
) ;
And We put Dummy data on 100 records in it .
Now Retrieve that data.i use limit property in my query like
SELECT id,name,mobno,address from record limit 0,6
it will limit data in only 6 rows.
Now by using String Query's i create a hyper links containing page no and then on page load get them and use in limit value .
Formula: ( page_no - 1 ) * 6
Query String: <a href='usman.php?page=".$s1."'>".$s1."</a>"
Get Request:if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
Total Records:
$sql1 = "SELECT COUNT(id) FROM record";
$result1 = $conn->query($sql1);
$getresult = $result1->fetch_assoc();
$total = $getresult["COUNT(id)"];
$s=$total/6;
CSS Code For Style:
<style>
span{padding:10px;margin:10px;
bottom: 5%;position: fixed;}
span a{margin: 20px;border: 1px solid green;padding: 10px;}
body{background-color: #eae;}
div{margin:5px auto;width: 50%;border: 5px solid;
background-color: skyblue; padding: 10px;}
div table{margin: auto;}
div table td{border: 5px solid green;}
</style>
PHP Full Code:
<?php
include "conn.php";
echo '<h1 style="text-align:center;">Pagging in PHP</h1>';
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$first = ($page-1) * 6;
$sql = "SELECT id,name,mobno,address from record limit $first,6" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc())
{
echo "<div><table border=\"1\">";
echo "<tr><th colspan=\"2\"width=200> Dummay Data For ".$row["id"]. "</th>";
echo "<th rowspan=\"2\">";
echo " Mobile No : ".$row["mobno"]."</th></tr>";
echo " <td>My Name: " .$row["name"]. "</td>";
echo " <td>Yes My Name " .$row["address"]. "</td></table></div>";
}}
$sql1 = "SELECT COUNT(id) FROM record";
$result1 = $conn->query($sql1);
$getresult = $result1->fetch_assoc();
$total = $getresult["COUNT(id)"];
$s=$total/6;
?>
<span>
<?php
for($s1=1;$s1<=$s;$s1++){
echo "<a href='usman.php?page=".$s1."'>".$s1."</a>";
}?>
</span>
<?php
$conn->close();
?>
Post a Comment