欢迎进入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; //否则,设置为第一页 }/> |
三、创建用例用表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))/> |
四、完整代码
<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>/> |
五、总结
<ccid_code></ccid_code>/> |
其实,写分页显示代码是很简单的,只要掌握了它的工作原理。

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

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

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),