我有大约10 000
个产品,我每页都会展示10
个产品,从而生成1000
页。
由于这个原因,我希望下面的分页示例中显示的页数限制:
Previous | 1 | 101 | 102 | 103 | 104 | ... | 1000 | Next
分页本身非常完美。显示的页面限制只是一个问题。
这是代码;
数据库:
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/* CONFIGURE PAGINATION */
/*
Set default page value to 0
*/
$current_page = 0;
/*
If $_GET['start'] exists, is a valid integer and is positive set $current_page to this value
*/
if (isset($_GET['start']) && is_int($_GET['start']) && $_GET['start'] >= 0) {
$current_page = $_GET['start'];
$current_page = $current_page * $max_rows;
}
/*
Set $max_rows to the number of records you want to show on each page
*/
$max_rows = 10;
/*
Set default values for pagination variables
*/
$total_rows = 0;
$total_pages = 1;
$prev_page = 0;
$next_page = 0;
$page_from = 1;
$page_to = 0;
// query to get messages from messages table
$connOne = "SELECT product_name, product_quantity, product_id, product_price_lowest, product_price_highest FROM product ORDER BY product_price_lowest ASC, product_price_highest DESC LIMIT $current_page,
$max_rows";
$stmt = $conn->prepare($connOne);
$stmt->execute();
if($stmt->rowCount() > 0){
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
}
/*
Your original row count query
*/
$count_query = "SELECT product_name, product_quantity, product_id, product_price_lowest, product_price_highest FROM product";
$stmt = $conn->prepare($count_query);
$stmt->execute();
$total_rows = $stmt->rowCount();
/*
Set $total_pages to ceiling of row count / max rows
*/
$total_pages = ceil($total_rows / $max_rows);
/*
If $current_page is higher than total pages, reset to $total_pages
*/
if ($current_page > $total_pages) $current_page = $total_pages;
/*
Set variables to control which page numbers are shown
*/
$page_to = $total_pages;
if ($current_page > 1) $prev_page = $current_page - 1;
if ($total_pages > $current_page) $next_page = $current_page + 1;
if ($total_pages > 5) {
if (($current_page - 3) > 1) $page_from = $current_page - 3;
if (($current_page + 3) < $total_pages) $page_to = $current_page + 3;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
PAGE OUTPUT:
<?php
/* OUTPUT TO PAGE */
/*
Check that we've actually got some records, although I assume you're
doing this elsewhere
*/
if ($total_rows > 0) {
echo '<ul>';
/*
If we're not on the first page show a link to page 1 and an ellipsis
*/
if ($prev_page > 0) {
echo '<li><a href="?page=' . $prev_page . '">Previous</a></li>';
}
if ($page_from != 0) {
echo '<li><a href="?page=0">First</a></li>';
echo '<li><span>. . . </span></li>';
}
/*
Loop through the page numbers that we're showing and set class="active" on the current page
*/
for ($p = $page_from; $p <= $page_to; $p++) {
echo '<li' . ($current_page == $p ? ' class="active"' : '') . '> <a href=?page=' . $p . '>' . $p . '</a> </li>';
}
/*
If we're not on the last page show an ellipsis and a link to the last page
*/
if ($page_to != $total_pages) {
echo '<li><span> . . . </span></li>';
echo '<li><a href="?page=' . $total_pages . '">' . $total_pages . '</a></li>';
}
if ($next_page > 0) {
echo '<li><a href="?page=' . $next_page . '">Next</a></li>';
}
echo '</ul>';
} else {
echo 'No records.';
}
答案 0 :(得分:0)
我觉得你有点困惑。试试这个 - 我在你更新的帖子中编辑了代码。在编辑代码以调整内容之前,请查看我在此处发布的内容的结果。
我已将$_GET['start']
替换为$_GET['page']
。 $_GET['page']
现在是页码,不是起始行。
<强>数据库:强>
<?php
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*
Set default page value to 1
*/
$current_page = 1;
/*
If $_GET['start'] exists, is a valid integer and is positive set $current_page to this value
*/
if (isset($_GET['page']) && is_int($_GET['page']) && $_GET['page'] >= 0) {
$current_page = $_GET['page'];
}
/*
Set $max_rows to the number of records you want to show on each page
*/
$max_rows = 10;
/*
Set default values for pagination variables - don't change this
*/
$total_rows = 0;
$total_pages = 1;
$prev_page = 0;
$next_page = 0;
$page_from = 1;
$page_to = 0;
/*
Your original row count query
*/
$count_query = "SELECT product_name, product_quantity, product_id, product_price_lowest, product_price_highest FROM product";
$stmt = $conn->prepare($count_query);
$stmt->execute();
$total_rows = $stmt->rowCount();
/*
Set $total_pages to ceiling of row count / max rows
*/
$total_pages = ceil($total_rows / $max_rows);
/*
If $current_page is higher than total pages, reset to $total_pages
*/
if ($current_page > $total_pages) $current_page = $total_pages;
/*
Set start row for DB query
*/
$db_start_row = ($current_page * $max_rows) - $max_rows;
// query to get messages from messages table
$connOne = "SELECT product_name, product_quantity, product_id, product_price_lowest, product_price_highest FROM product ORDER BY product_price_lowest ASC, product_price_highest DESC LIMIT $db_start_row, $max_rows";
$stmt = $conn->prepare($connOne);
$stmt->execute();
if($stmt->rowCount() > 0){
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
}
/*
Set variables to control which page numbers are shown
*/
$page_to = $total_pages;
if ($current_page > 1) $prev_page = $current_page - 1;
if ($total_pages > $current_page) $next_page = $current_page + 1;
if ($total_pages > 5) {
if (($current_page - 2) > 1) $page_from = $current_page - 2;
if (($current_page + 2) < $total_pages) $page_to = $current_page + 2;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
PAGE OUTPUT:
<?php
/*
* Check that we've actually got some records, although
* I assume you're doing this elsewhere
*/
if ( $total_rows > 0 ) {
echo '<ul>';
/*
* If we're not on the first page show a link to page 1 and an ellipsis
*/
if ( $prev_page > 0 ) {
echo '<li><a href="?page=' . $prev_page . '">Previous</a></li>';
}
if ( $page_from != 1 ) {
echo '<li><a href="?page=1">1</a></li>';
echo '<li><span>...</span></li>';
}
/*
* Loop through the page numbers that we're showing and set class="active"
* on the current page
*/
for ( $p = $page_from; $p <= $page_to; $p++ ) {
echo '<li' . ( $current_page == $p ? ' class="active"' : '' ) . '><a href=?page=' . $p . '>' . $p . '</a></li>';
}
/*
* If we're not on the last page show an ellipsis and a link to the last page
*/
if ( $page_to != $total_pages ) {
echo '<li><span>...</span></li>';
echo '<li><a href="?page=' . $total_pages . '">' . $total_pages . '</a></li>';
}
if ( $next_page > 0 ) {
echo '<li><a href="?page=' . $next_page . '">Next</a></li>';
}
echo '</ul>';
} else {
echo 'No records.';
}
?>