im trying to learn pagination with PHP/PDO.
$limit = 20;
$sth = $conn->prepare(\"SELECT * FROM directory WHERE user_active != \'\'
Please use Below code for Pagination using PDO:
Simple Pagination Demo using PDO Query
getMessage(); }
// No. of adjacent pages shown on each side
$adjacents = 2;
// We will assign variable here for entry By. you can use your variables here.
$EntryBy = "furqan.aziz";
// We Will prepare SQL Query
$STM = $dbh->prepare("SELECT ServerName FROM statstracker WHERE EntryBy = :EntryBy");
// bind paramenters, Named paramenters alaways start with colon(:)
$STM->bindParam(':EntryBy', $EntryBy);
// For Executing prepared statement we will use below function
$STM->execute();
// Count no. of records
$Records = $STM->rowCount();
// Your File Name will be the same like your php page name which is index.php
$targetpage = "index.php";
// Below is setting for no. of records per page.
$limit = 10;
$page = $_GET['page'];
if($page)
//First Item to dipaly on this page
$start = ($page - 1) * $limit;
else
//if no page variable is given, set start to 0
$start = 0;
// Get data using PDO prepare Query.
$STM2 = $dbh->prepare("SELECT `SrNo`, `ServerName`, `HiMemUti`, `AvgMemUti`, `HiCpuUti`, `AvgCpuUti`, `HiIOPerSec`, `AvgIOPerSec`, `HiDiskUsage`, `AvgDsikUsage`, `EntryBy` FROM statstracker WHERE EntryBy = :EntryBy ORDER BY SrNo LIMIT $start, $limit");
// bind paramenters, Named paramenters alaways start with colon(:)
$STM2->bindParam(':EntryBy', $EntryBy);
// For Executing prepared statement we will use below function
$STM2->execute();
// We will fetch records like this and use foreach loop to show multiple Results later in bottom of the page.
$STMrecords = $STM2->fetchAll();
// Setup page variables for display. If no page variable is given, default to 1.
if ($page == 0) $page = 1;
//previous page is page - 1
$prev = $page - 1;
//next page is page + 1
$next = $page + 1;
//lastpage is = total Records / items per page, rounded up.
$lastpage = ceil($Records/$limit);
//last page minus 1
$lpm1 = $lastpage - 1;
//Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once.
$pagination = "";
if($lastpage > 1)
{
$pagination .= "";
//previous button
if ($page > 1)
$pagination.= "Previous";
else
$pagination.= "Previous";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "$counter";
else
$pagination.= "$counter";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "$counter";
else
$pagination.= "$counter";
}
$pagination.= "...";
$pagination.= "$lpm1";
$pagination.= "$lastpage";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "1";
$pagination.= "2";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "$counter";
else
$pagination.= "$counter";
}
$pagination.= "...";
$pagination.= "$lpm1";
$pagination.= "$lastpage";
}
//close to end; only hide early pages
else
{
$pagination.= "1";
$pagination.= "2";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "$counter";
else
$pagination.= "$counter";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "Next";
else
$pagination.= "Next";
$pagination.= "\n";
}
//Below is a start of table in which we will show records using foreach loop.
echo "
echo $pagination;
// Closing MySQL database connection
$dbh = null;
?>
For more details please visit below sites for source code and CSS styles
Click here!
Click here!
- 热议问题