PHP 分頁在網站開發中扮演重要角色,能夠幫助網頁內容分頁顯示,提升使用者體驗。 php小編西瓜將為大家介紹如何使用PHP實現分頁功能,讓網站更容易瀏覽與管理。透過本文的學習,讀者可以輕鬆掌握PHP分頁的原理和實作方法,為網站開發增添新的技能和靈感。
我們也會示範另一種在 PHP 分頁中新增 Prev<strong class="keylink">io</strong>us
和 Next
導覽功能的方法。此方法僅新增了將相應頁面導航到第一種方法的附加功能。
SQL
中使用LIMIT
子句和SELECT
語句對行進行分頁#我們可以使用LIMIT
子句和SELECT
語句來指定要在頁面上顯示的前n 個結果。我們可以在 anchor
標籤中提供頁碼作為 GET
方法來瀏覽頁面。在此方法中,我們定義每頁要顯示的行數,並從資料庫#中檢索所有行以計算所需的總頁數。我們可以使用 $_GET
陣列和 isset()
函數來取得使用者請求的頁碼。
例如,建立一個變數 $results_per_page
並在其中儲存 2
。使用 <strong class="keylink">Mysql</strong>i_num_rows()
函數尋找資料庫中的行數並儲存在 $number_of_results
變數中。使用 ceil()
函數來決定顯示行所需的總頁數。將 $number_of_results
變數除以 ceil()
函數內的 $results_per_page
變數。使用 $_GET
超全域變數和 isset()
函數來檢查 page
變數是否已設定。如果尚未設置,請將 $page
變數設為 1
。如果已設定變量,則將值指派給 $page
變數。以 1
減去 $page
變數並將其乘以 $this_page_first_result
變數。將操作儲存在變數 $this_page_first_result
中。寫一個帶有LIMIT
子句的SQL
語句為SELECT * FROM alpha LIMIT ' . $this_page_first_result . ',' . $results_per_page.
執行查詢並顯示顯示結果。最後,建立一個 for
迴圈以在 $page
變數和 $number_of_page
之間循環。在循環內,echo
anchor
標籤並將 href
# 屬性的值設為 index.php?page'.$page
。在 anchor
標籤之間寫入 $page
變數。
在下面的範例中,資料庫中的 alpha
表包含六行。執行分頁後,每頁顯示兩行。 ceil()
函數決定顯示行所需的總頁數。最初,$page
變數未設置,因此頁面從第 1 頁開始。 $this_page_first_result
變數決定使用者目前所在的頁碼。變數 $this_page_first_result
表示頁面的第一行。變數 $results_per_page
表示每頁的結果數。輸出部分顯示 index.php
頁面。當點擊頁面 2
時,它會從資料庫中輸出接下來的兩行。
範例程式碼:
<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#408080;font-style:italic">#php 7.x </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#666"><?</span>php </span></span><span style="display:flex;"><span><span style="color:#19177c">$number_of_pages</span> <span style="color:#666">=</span> ceil(<span style="color:#19177c">$number_of_results</span><span style="color:#666">/</span><span style="color:#19177c">$results_per_page</span>); </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">if</span> (<span style="color:#666">!</span>isset(<span style="color:#19177c">$_GET</span>[<span style="color:#ba2121">'page'</span>])) { </span></span><span style="display:flex;"><span><span style="color:#19177c">$page</span> <span style="color:#666">=</span> <span style="color:#666">1</span>; </span></span><span style="display:flex;"><span>} <span style="color:#008000;font-weight:bold">else</span> { </span></span><span style="display:flex;"><span><span style="color:#19177c">$page</span> <span style="color:#666">=</span> <span style="color:#19177c">$_GET</span>[<span style="color:#ba2121">'page'</span>]; </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span><span style="color:#19177c">$this_page_first_result</span> <span style="color:#666">=</span> (<span style="color:#19177c">$page</span><span style="color:#666">-</span><span style="color:#666">1</span>)<span style="color:#666">*</span><span style="color:#19177c">$results_per_page</span>; </span></span><span style="display:flex;"><span><span style="color:#19177c">$sql</span><span style="color:#666">=</span><span style="color:#ba2121">'SELECT * FROM alpha LIMIT '</span> <span style="color:#666">.</span> <span style="color:#19177c">$this_page_first_result</span> <span style="color:#666">.</span> <span style="color:#ba2121">','</span> <span style="color:#666">.</span> <span style="color:#19177c">$results_per_page</span>; </span></span><span style="display:flex;"><span><span style="color:#19177c">$result</span> <span style="color:#666">=</span> <strong class="keylink">mysql</strong>i_query(<span style="color:#19177c">$con</span>, <span style="color:#19177c">$sql</span>); </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">while</span>(<span style="color:#19177c">$row</span> <span style="color:#666">=</span> mysqli_fetch_array(<span style="color:#19177c">$result</span>)) { </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$row</span>[<span style="color:#ba2121">'id'</span>] <span style="color:#666">.</span> <span style="color:#ba2121">' '</span> <span style="color:#666">.</span> <span style="color:#19177c">$row</span>[<span style="color:#ba2121">'value'</span>]<span style="color:#666">.</span> <span style="color:#ba2121">'<br>'</span>; </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">for</span> (<span style="color:#19177c">$page</span><span style="color:#666">=</span><span style="color:#666">1</span>;<span style="color:#19177c">$page</span><span style="color:#666"><=</span><span style="color:#19177c">$number_of_pages</span>;<span style="color:#19177c">$page</span><span style="color:#666">++</span>) { </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">'<a href="index.php?page='</span> <span style="color:#666">.</span> <span style="color:#19177c">$page</span> <span style="color:#666">.</span> <span style="color:#ba2121">'">'</span> <span style="color:#666">.</span> <span style="color:#19177c">$page</span> <span style="color:#666">.</span> <span style="color:#ba2121">'</a> '</span>; </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span > </span></span></span></code></code>
輸出:
<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>1 A </span></span><span style="display:flex;"><span>2 B </span></span><span style="display:flex;"><span>1 2 3 </span></span></code></code>
Previous
和Next
導航功能我們可以在第一種方法的程式碼範例中添加一些額外的程式碼片段,以在分頁中提供Previous
和Next
導覽功能。我們可以將 $page
變數遞增和遞減 1 以指向上一頁和下一頁。我們可以使用 anchor
標籤中的遞增和遞減變數來實現 Next
和 Previous
功能。
例如,建立兩個變量,$prev
和 $next
。用 1 減去 $page
變數並將操作指派給 $prev
變數。類似地,將 1 加到 $page
變數並將其指派給 $next
變數。在頁碼的 for
迴圈之前回顯一個 anchor
標籤,上面寫著 Previous
。將 anchor
標籤的 href
屬性賦值為 index.php?page=' . $prev .
。以相同的方式,使用$next
變數作為href
屬性中page
的值,為Next
建立另一個anchor
標記。
在下面的輸出部分,它顯示了第二頁。點選 Previous
進入第一頁,點選 Next
進入第三頁。
範例程式碼:
<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#408080;font-style:italic"># php 7.x </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#666"><?</span>php </span></span><span style="display:flex;"><span><span style="color:#19177c">$prev</span> <span style="color:#666">=</span> <span style="color:#19177c">$page</span> <span style="color:#666">-</span><span style="color:#666">1</span>; </span></span><span style="display:flex;"><span><span style="color:#19177c">$next</span> <span style="color:#666">=</span> <span style="color:#19177c">$page</span> <span style="color:#666">+</span><span style="color:#666">1</span>; </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">' <a href="index.php?page='</span> <span style="color:#666">.</span> <span style="color:#19177c">$prev</span> <span style="color:#666">.</span> <span style="color:#ba2121">'"> Previous </a> '</span>; </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">for</span> (<span style="color:#19177c">$page</span><span style="color:#666">=</span><span style="color:#666">1</span>;<span style="color:#19177c">$page</span><span style="color:#666"><=</span><span style="color:#19177c">$number_of_pages</span>;<span style="color:#19177c">$page</span><span style="color:#666">++</span>) { </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">'<a href="index.php?page='</span> <span style="color:#666">.</span> <span style="color:#19177c">$page</span> <span style="color:#666">.</span> <span style="color:#ba2121">'">'</span> <span style="color:#666">.</span> <span style="color:#19177c">$page</span> <span style="color:#666">.</span> <span style="color:#ba2121">'</a> '</span>; </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">' <a href="index.php?page='</span> <span style="color:#666">.</span> <span style="color:#19177c">$next</span> <span style="color:#666">.</span> <span style="color:#ba2121">'"> Next </a> '</span>; </span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span > </span></span></span></code></code>
輸出:
<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>3 C </span></span><span style="display:flex;"><span>4 D </span></span><span style="display:flex;"><span>Previous 1 2 3 Next </span></span></code></code>
以上是PHP 分頁的詳細內容。更多資訊請關注PHP中文網其他相關文章!