search
HomeBackend DevelopmentPHP TutorialPHP从0单排(十四)数据分页显示的原理及实现

PHP从零单排(十四)数据分页显示的原理及实现

分页显示是WEB编程中最频繁处理的环节之一。所谓分页显示,就是通过程序将结果集一段一段的来显示。实现分页显示,需要两个初始参数:每页显示多少记录和当前是第几页。再加上完整的结果集,就可以实现数据的分页显示。至于其他功能,比如上一页、下一页等均可以根据以上信息加以处理得到。

要取得某表中的前10条记录,可以使用如下SQL语句:

SELECT * FROM a_table LIMIT 0,10

要查找第11到第20条记录,使用的SQL语句如下所示:

SELECT * FROM a_table LIMIT 10,10

如要查找第21条到第30条记录,使用的SQL语句如下所示:

SELECT * FROM a_table LIMIT 20,10

以上SQL语句可以看出,每次取10条记录,相当于每个页面显示10条数据,而每次所要取得记录的起始位置和当期页数之间存在着这样的关系:起始位置=(当前页数-1)*每页要显示的记录数。如果以变量$page_size表示每页显示的记录数,以变量$cur_page表示当前页数,那么上述可以用下面所示的SQL语句模板归纳:

select * from table limit ($cur_page-1)*$page_size,$page_size;

这样,就得到了分页情况下获取数据的SQL语句。其中$page_size可以根据实际情况制定为一个定值,实际开发中,当前页面$cur_page可以由参数传入。另外,数据要显示的总页数,可以在记录总数和每页显示的记录数之间通过计算获得。比如,如果总记录数除以每页显示的记录数后,没有余数,那么总页数就是这二者之商。

<?php $host='localhost';$user_name='root';$password='helloworld';$conn=mysql_connect($host,$user_name,$password);if(!$conn){	die('FAIL!'.mysql_error());}mysql_select_db('test');if(isset($_GET['page'])){	$page=$_GET['page'];}else{	$page=1;}$page_size=2;$sql='select * from users';$result=mysql_query($sql);$total=mysql_num_rows($result);if($total){	if($total<$page_size)	$page_count=1;	if($total%$page_size)	{		$page_count=(int)($total/$page_size)+1;	}	else	{		$page_count=$total/$page_size;	}}else{	$page_count=0;}$turn_page='';if($page==1){	$turn_page.='Index | Before |';}else{	$turn_page.='<a href=13-8.php?page=1>Index | <a href="13-8.php?page='.(%24page-1).'">Before</a> |';}if($page==$page_count || $page_count==0){	$turn_page.='Next | Last';}else{	$turn_page.='<a href="13-8.php?page='.(%24page+1).'"> Next </a> | <a href="13-8.php?page='.%24page_count.'"> Last </a>';}$sql='select id,name,sex,age from users limit '.($page-1)*$page_size.','.$page_size;$result=mysql_query($sql) OR die ("<br>ERROR:<b>".mysql_error()."</b><br>SQL:".$sql);?><title>13-8.php</title>

**********************

POST GET ,是提交表单的两种方式,GET传值就用$_GET获取,POST提交表单就用$_POST
post与get的区别是一个在地址栏显示参数,另一个不显示

举个例子,如果你登录的时候用get方式,那么你的值就会在地址栏上显示,这样就无安全性可言
而你在搜索或者有页码的时候 用post把参数在地址栏上隐藏起来,这样就毫无意义

而用$_GET可以获得浏览器地址栏上的参数的值(?问号后面的一串字符),比如www.baidu.com/s?wd=123,那么你用$_GET,就可以获取参数(你可以理解为事件,动作,或者参数,该值在传递表单时与input的name一致)为wd的值123,多个参数用&符连接,比如?an=0&si=5理解为an参数的值为0与si参数的值为5。

**********************

打个比方说,你输入一个地址叫 www.iron-feet.cn/?page=2
$_GET["page"]就是获得地址上这个page的值,即得到2

ID
Name
Sex
Age
          
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
华为GT3 Pro和GT4的差异是什么?华为GT3 Pro和GT4的差异是什么?Dec 29, 2023 pm 02:27 PM

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

修复:截图工具在 Windows 11 中不起作用修复:截图工具在 Windows 11 中不起作用Aug 24, 2023 am 09:48 AM

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

如何修复无法连接到iPhone上的App Store错误如何修复无法连接到iPhone上的App Store错误Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

mysql怎么删除unique keymysql怎么删除unique keyMay 12, 2022 pm 03:01 PM

在mysql中,可利用“ALTER TABLE 表名 DROP INDEX unique key名”语句来删除unique key;ALTER TABLE语句用于对数据进行添加、删除或修改操作,DROP INDEX语句用于表示删除约束操作。

mysql需要commit吗mysql需要commit吗Apr 27, 2022 pm 07:04 PM

在mysql中,是否需要commit取决于存储引擎:1、若是不支持事务的存储引擎,如myisam,则不需要使用commit;2、若是支持事务的存储引擎,如innodb,则需要知道事务是否自动提交,因此需要使用commit。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft