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
。使用 <code><strong class="keylink">Mysql</strong>i_num_rows()
Mysqli_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
函数确定显示行所需的总页数。最初,$this_page_first_result
变量未设置,因此页面从第 1 页开始。$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
在 PHP 的分页中添加 Next
和 Previous
我们可以在第一种方法的代码示例中添加一些额外的代码片段,以在分页中提供 Next
和 $page
导航功能。我们可以将 anchor
变量递增和递减 1 以指向上一页和下一页。我们可以使用 Next
标签中的递增和递减变量来实现 Previous
和
$prev
例如,创建两个变量,$next
和 $page
。用 1 减去 $prev
变量并将操作分配给 $page
变量。类似地,将 1 添加到 $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中文网其他相关文章!