All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Asp.net获得日历控件当前选择显示的年月_.net资料_编程技术
GridView隐藏列取值解决方案_[Asp.Net教程]
SQL循序渐进(24)嵌入SQL_[SQL,Server教程]
数据仓库数据建模的几种思路_mssql学习_编程技术
在Windows,xp系统上安装了SQL,server服务器版_[SQL,Server教程]
asp.net验证码控件_[Asp.Net教程]
[delphi语法4]delphi中if语句的使用实例
升级到SQL Server 2005的常见问题解答_[SQL Server教程]
详解:——如何将图片储存在数据库里_php资料_编程技术
asp.net用代码压缩服务器上文件夹或文件_[Asp.Net教程]
Reading number is top 10 pictures
Players in the eyes of a perfect love2
A man's favorite things15
清醇靓丽的美眉
Sell the barbecue as says father du breul1
Get girl by your hand
Steal to eat bacon bird
接财神,大吉大利,财源滚滚来
A beautiful girl to bud1
狗狗与主人神同步1
Thrilling English baby
Download software ranking
星际争霸1.08硬盘免安装版
C#程序员参考手册
软件工程思想
电车之狼R
Unix video tutorial13
Red cliff
Unix video tutorial5
matrix3
Unix video tutorial9
Boxer's Top ten classic battle7
归海一刀 published in(发表于) 2014/2/10 6:50:54 Edit(编辑)
web开发中PHP+MySQL分页显示示例分析_[PHP教程]

web开发中PHP+MySQL分页显示示例分析_[PHP教程]

web开发中PHP+MySQL分页显示示例分析_[PHP教程]

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条记录。

  二、主要代码解析


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


create table myTable(id int NOT NULL auto_increment,news_title varchar(50),news_cont text,add_time datetime,PRIMARY KEY(id))


  四、完整代码


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


  五、总结

  本例代码在windows2000 server+php4.4.0+mysql5.0.16上运行正常。该示例显示的分页格式是[1][2][3]…这样形式。假如想显示成“首页 上一页 下一页 尾页”这样形式,请加入以下代码:


first=1;
prev=page-1;
next=page+1;
last=pages;

if (page > 1)
{
 echo "<a href='fenye.php?page=".first."'>首页</a> ";
 echo "<a href='fenye.php?page=".prev."'>上一页</a> ";
}

if (page < pages)
{
 echo "<a href='fenye.php?page=".next."'>下一页</a>
 echo "<a href='fenye.php?page=".last."'>尾页</a> ";
}


  其实,写分页显示代码是很简单的,只要掌握了它的工作原理。希望这篇文章能够带给那些需要这方面程序web程序员的帮助。





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.