完全讲解PHP+MySQL的分页显示示例分析
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 Web开发是今后分布式程式开发的主流,通常的web开发都要涉及到与数据库打交道,客户端从服务器端读取通常都是以分页的形式来显示,一页一页的阅读起来既方便又美观。所以说写分页程序是web开发的一个
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
Web开发是今后分布式程式开发的主流,通常的web开发都要涉及到与数据库打交道,客户端从服务器端读取通常都是以分页的形式来显示,一页一页的阅读起来既方便又美观。所以说写分页程序是web开发的一个重要组成部分,在这里,我们共同来研究分页程序的编写。
一、分页程序的原理
分页程序有两个非常重要的参数:每页显示几条记录($pagesize)和当前是第几页($page)。有了这两个参数就可以很方便的写出分页程序,我们以MySql数据库作为数据源,在MySQL里如果要想取出表内某段特定内容可以使用的T-SQL语句:select * from table limit offset,rows来实现。这里的offset是记录偏移量,它的计算方法是offset=$pagesize*($page-1),rows是要显示的记录条数,这里就是$page。也就是说select * from table limit 10,10这条语句的意思是取出表里从第11条记录开始的20条记录。
二、主要代码解析
<ccid_code></ccid_code>$pagesize=10; //设置每一页显示的记录数 $conn=mysql_connect("localhost","root",""); //连接数据库 $rs=mysql_query("select count(*) from tb_product",$conn); //取得记录总数$rs $myrow = mysql_fetch_array($rs); $numrows=$myrow[0]; //计算总页数 $pages=intval($numrows/$pagesize); //判断页数设置 if (isset($_GET['page'])){ $page=intval($_GET['page']); } else{ $page=1; //否则,设置为第一页 }/> Copy after login |
三、创建用例用表myTable
<ccid_code></ccid_code>create table myTable (id int NOT NULL auto_increment,news_title varchar(50), news_cont text,add_time datetime,PRIMARY KEY(id))/> Copy after login |
四、完整代码
<ccid_code></ccid_code><html> <head> <title>php分页示例</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <?php $conn=mysql_connect("localhost","root",""); //设定每一页显示的记录数 $pagesize=1; mysql_select_db("mydata",$conn); //取得记录总数$rs,计算总页数用 $rs=mysql_query("select count(*) from tb_product",$conn); $myrow = mysql_fetch_array($rs); $numrows=$myrow[0]; //计算总页数 $pages=intval($numrows/$pagesize); if ($numrows%$pagesize) $pages++; //设置页数 if (isset($_GET['page'])){ $page=intval($_GET['page']); } else{ //设置为第一页 $page=1; } //计算记录偏移量 $offset=$pagesize*($page - 1); //读取指定记录数 $rs=mysql_query("select * from myTable order by id desc limit $offset,$pagesize",$conn); if ($myrow = mysql_fetch_array($rs)) { $i=0; ?> <table border="0" width="80%"> <tr> <td width="50%" bgcolor="#E0E0E0"> <p align="center">标题</td> <td width="50%" bgcolor="#E0E0E0"> <p align="center">发布时间</td> </tr> <?php do { $i++; ?> <tr> <td width="50%"><?=$myrow["news_title"]?></td> <td width="50%"><?=$myrow["news_cont"]?></td> </tr> <?php } while ($myrow = mysql_fetch_array($rs)); echo "</table>"; } echo "<div align='center'>共有".$pages."页(".$page."/".$pages.")"; for ($i=1;$i< $page;$i++) echo "<a href='fenye.php?page=".$i."'>[".$i ."]</a> "; echo "[".$page."]"; for ($i=$page+1;$i<=$pages;$i++) echo "<a href='fenye.php?page=".$i."'>[".$i ."]</a> "; echo "</div>"; ?> </body> </html>/> Copy after login |
五、总结
<ccid_code></ccid_code>/> Copy after login |
其实,写分页显示代码是很简单的,只要掌握了它的工作原理。

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



Indentation specifications and examples of Go language Go language is a programming language developed by Google. It is known for its concise and clear syntax, in which indentation specifications play a crucial role in the readability and beauty of the code. effect. This article will introduce the indentation specifications of the Go language and explain in detail through specific code examples. Indentation specifications In the Go language, tabs are used for indentation instead of spaces. Each level of indentation is one tab, usually set to a width of 4 spaces. Such specifications unify the coding style and enable teams to work together to compile

The DECODE function in Oracle is a conditional expression that is often used to return different results based on different conditions in query statements. This article will introduce the syntax, usage and sample code of the DECODE function in detail. 1. DECODE function syntax DECODE(expr,search1,result1[,search2,result2,...,default]) expr: the expression or field to be compared. search1,

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

Recently, Samsung Display and Microsoft signed an important cooperation agreement. According to the agreement, Samsung Display will develop and supply hundreds of thousands of OLEDoS panels for mixed reality (MR) head-mounted devices to Microsoft. Microsoft is developing an MR device for multimedia content such as games and movies. This device is expected to It will be launched after the OLEDoS specifications are finalized, mainly serving the commercial field, and is expected to be delivered as early as 2026. OLEDoS (OLED on Silicon) technology OLEDoS is a new display technology that deposits OLED on a silicon substrate. Compared with traditional glass substrates, it is thinner and has higher pixels. OLEDoS display and ordinary display

In Linux systems, you can use the pwd command to display the current path. The pwd command is the abbreviation of PrintWorkingDirectory and is used to display the path of the current working directory. Enter the following command in the terminal to display the current path: pwd After executing this command, the terminal will display the full path of the current working directory, such as: /home/user/Documents. In addition, you can use some other options to enhance the functionality of the pwd command. For example, the -P option can display

Title: Analysis of the reasons and solutions for why the secondary directory of DreamWeaver CMS cannot be opened. Dreamweaver CMS (DedeCMS) is a powerful open source content management system that is widely used in the construction of various websites. However, sometimes during the process of building a website, you may encounter a situation where the secondary directory cannot be opened, which brings trouble to the normal operation of the website. In this article, we will analyze the possible reasons why the secondary directory cannot be opened and provide specific code examples to solve this problem. 1. Possible cause analysis: Pseudo-static rule configuration problem: during use

There are two most common ways to paginate PHP arrays: using the array_slice() function: calculate the number of elements to skip, and then extract the specified range of elements. Use built-in iterators: implement the Iterator interface, and the rewind(), key(), current(), next(), and valid() methods are used to traverse elements within the specified range.

In Linux, URL or Curl client is a popular command line utility that allows you to transfer data over the network using various protocols such as HTTPS, HTTP, FTP, etc. It allows you to send and receive data using its get, post and request methods. Among them, you need to use the "get" method frequently. Therefore, it becomes crucial to learn various methods and various options that you can use to increase your productivity. "Performing a curl operation is as simple as entering a few simple commands. Although it seems simple, many users do not fully realize its potential. Therefore, this short guide provides some information on how to perform curl operations on Linux systems. Example using the "curlget" command." Curl
