分页代码PHP / HTML:
<?php
//Connecting To The DB And Fetching Number Of Rows.
$stmt = $pdo->prepare('SELECT * FROM locations');
$stmt->execute();
$count = $stmt->rowCount();
$values = $stmt->fetchAll();
//Check If Page Number Is Set To Specific Value.
if (isset($_GET['pageno']))
{
$pageno = $_GET['pageno']; //Current Page Number.
$no_of_records_per_page = 100; // Number Of Rows Per Page.
$total_pages = ceil($count / $no_of_records_per_page); //Total Number Of Pages.
$offset = ($pageno-1) * $no_of_records_per_page; //Starting From.
}
//If Page Number Not Specified The First Page Will Be Shown.
else
{
$pageno = 1;
}
?>
<ul class="pagination">
//First Page.
<li>
<a href="?pageno=1">First</a>
</li>
//Previous Page.
<li>
<a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a>
</li>
<li>
//Loop Through Pages.
<?php
for ($i=1; $i <= $total_pages ; $i++)
{
echo '<a href="?pageno='.$i.'">'.$i.'</a>';
}
?>
</li>
//Next Page.
<li>
<a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a>
</li>
//Last Page.
<li>
<a href="?pageno=<?php echo $total_pages; ?>">Last</a>
</li>
</ul>
现在它显示了php页面中的所有页面。
______ ______ ______ _______ ________ _______
| | | | | | | | | | | |
| 1 | | 2 | | 3 | | 4 | | 5 | | 6 |
|______| |______| |______| |______| |______ | |______| ..
我想只显示特定数量的页面,如5或7,当我转到下一页时,新页面显示如下:
1 2 3 4 5 .... 30 //When I Go To The First Page , 30 Is the last page.
1 . . 4 5 6 .. 30 //When I Go To Page Number 5.
1 . . 27 28 29 30 //When I Go To The Last Page.
如何实现与此类似的东西?
答案 0 :(得分:0)
if ($pageno > 4) {
echo '<a>1st page link</a>';
echo '...';
}
for ($i = $pageno-3; $i <= $pageno+3; $i++) {
if ($i <= 0 OR $i == $pageno) continue;
if ($i > $total_pages) break;
echo '<a>#1 page link</a>';
}
if ($pageno < $total_pages-4) {
echo '...';
echo '<a>last page link</a>';
}