私は約10 000
製品を持っています。ここにはがあります。その結果、ページあたり10
個の製品が表示され、1000
ページが表示されます。自体は完璧に動作しページネーション - 見えるページ番号を制限する
Previous | 1 | 101 | 102 | 103 | 104 | ... | 1000 | Next
ページネーション:
は、このような理由のために、私は以下のページネーションサンプルのように示されたページ数の制限をしたいと思います。表示されているページの制限のみが問題です。
ここにコードがあります。
DATABASE:
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;
?>
ページ出力:
<?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.';
}
あなたが少し良く質問を明確にしてくださいもらえますか? – Difster
私が言ったように、私は約1000ページを持っており、すべてのページへのリンクがすべて見えています。 1,2,3,4,5,6,7,8,9,10,11,12,13などを1000に上げる代わりに、私は5ページのリンクを "Previous"と"次へ"ボタン。たとえば、「PREVIOUS - 1 ... 11,12,13,14,15 .... 1000 - NEXT」のようにします。 – Swift123
いくつかのコードを書くのを手助けする前に、いくつかの指針を示して、どれだけ最初に自分自身を理解できるかを見てみましょう。 現在のページ番号を取り込んで別の関数を作成し、次に5ページの範囲を返します。前と次のボタンは、現在のページ番号が+または - 1であることを明示的に示します。新しいページが読み込まれるたびに、次の範囲を取得する関数を呼び出します。 – Difster