PHP MySQLi 分页限制页数

问题描述 投票:0回答:1

我有一个简单的 PHP MySQLi 分页脚本,每页显示 5 个条目。但对于大型数据库条目,会显示相当多的页码。如何限制分页中显示的页数?例如。一次 10 页: 第一个 < 1 2 3 4 5 6 7 8 9 10 > 最后一个

这是我的代码:

<!DOCTYPE html><html>
<head>
<title>PHP Pagination</title>
</head>

<body>
<?php
// Establish Connection to the Database
$con = mysqli_connect('localhost','root','','classicmodels');

//Records per page
$per_page = 5;

if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page = 1;
}

// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;

//Selecting the data from table but with limit
$query = "SELECT * FROM customers LIMIT $start_from, $per_page";
$result = mysqli_query ($con, $query);

?>

<table align="center" border="2″ cellpadding="3″>
<tr><th>Name</th><th>Phone</th><th>Country</th></tr>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr align="center">
<td><?php echo $row['contactFirstName']; ?></td>
<td><?php echo $row['contactLastName']; ?></td>
</tr>
<?php
};
?>
</table>

<div>
<?php

//Now select all from table
$query = "select * from customers";
$result = mysqli_query($con, $query);

// Count the total records
$total_records = mysqli_num_rows($result);

//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);

//Going to first page
if($_GET['page'] != 1) {
echo "<center><a href='index.php?page=1'>".'First Page'."</a>";
} else echo "<center>";

echo "<a href='index.php?page=" . ($_GET['page']+1) . "'>Next Page</a>";

for ($i=1; $i<=$total_pages; $i++) {

echo "<a href='index.php?page=".$i."'>".$i."</a> ";
};

echo "<a href='index.php?page=" . ($_GET['page']-1) . "'>Previous Page</a>";

// Going to last page
if($_GET['page'] != $total_pages) {
echo "<a href='index.php?page=$total_pages'>".'Last Page'."</a></center> ";
} else echo "</center>";
?>

</div>

</body>
</html>
php mysqli pagination
1个回答
1
投票

你可以像这样实现一个分页器(简单版本,需要改进):

<?php

$totalPages = 28; //replace with database value
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$numPagesToShow = 10; //it's up to you

//avoid bug or trying to get page out of the bounds
if($currentPage > $totalPages) {
    $currentPage = $totalPages;
}

/* correct the number of pages to show on the left or right
 *   and always try to put the current page in the middle
 */
if($numPagesToShow >= $totalPages) {
    $numMaxPageLeft = 1;
    $numMaxPageRight = $totalPages;
} else {
    $pagesToShow = ceil($numPagesToShow/2);
    $numMaxPageLeft = $currentPage - $pagesToShow;
    $numMaxPageRight = $currentPage + $pagesToShow;

    if($numMaxPageLeft <= 0) {
        $numMaxPageRight = $numMaxPageRight - $numMaxPageLeft +1;
        $numMaxPageLeft = 1;
    } elseif($numMaxPageRight >= $totalPages) {
        $numMaxPageLeft -= ($numMaxPageRight - $totalPages);
        $numMaxPageRight = $totalPages;
    }
}

//loop to show all desired pages
for ($i=$numMaxPageLeft; $i<=$numMaxPageRight; $i++) {
    echo $i == $currentPage ? $i : "<a href='index.php?page=".$i."'>".$i."</a> ";
}
© www.soinside.com 2019 - 2024. All rights reserved.