PHP从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>
ID |
Name |
Sex |
Age |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings
