2017-07-11 16 views
0

私は約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.'; 
} 
+1

あなたが少し良く質問を明確にしてくださいもらえますか? – Difster

+0

私が言ったように、私は約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

+0

いくつかのコードを書くのを手助けする前に、いくつかの指針を示して、どれだけ最初に自分自身を理解できるかを見てみましょう。 現在のページ番号を取り込んで別の関数を作成し、次に5ページの範囲を返します。前と次のボタンは、現在のページ番号が+または - 1であることを明示的に示します。新しいページが読み込まれるたびに、次の範囲を取得する関数を呼び出します。 – Difster

答えて

0

私はあなたがビットのカップルと少し混乱しましたと思います。これを試してください - あなたの更新された投稿のコードを編集しました。物を微調整するコードを編集する前に、私がここに投稿したものでどのような結果が得られるかを見てください。

$_GET['start']$_GET['page']に置き換えました。 $_GET['page']はページ番号になり、ではなく、の開始行になります。

DATABASE:

<?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; 
?> 

ページ出力:

<?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.'; 
} 

?> 
+0

あなたのコードを試してみましたが、ページの量が制限されています。しかし今、リンクはもう働きません。たとえば、NEXTまたは特定のページ番号を押すと、テーブルは同じままです。テーブルが情報を変更する唯一の時間は、コード内で手作業で "$ current_page = 0;"を別の値に変更したときです。これは0が静的で、if($ total_pages> $ current_page)$ next_page = $ current_page + 1;で+1を追加することを考慮していないかのようです。注意してください、私は最初の投稿でコードを更新したので、どのように見えるかを見ることができます。 – Swift123

+0

'$ _GET ['start']'はページ番号か開始行ですか?だから、もし私が4ページ目にいるなら、 '$ _GET ['start']' = 4か30か? – Louis

+0

'$ current_page'を変更した行を追加しました。これは、ページ分割の残りの部分を締めました。私はあなたの余分なDBのものを含めるために私の答えを編集し、うまくいくはずです。 – Louis

関連する問題