簡體   English   中英

PHP MySQLi分頁限制頁數

[英]PHP MySQLi Pagination Limit Amount of Pages

我有一個簡單的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

$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> ";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM