Discuz教程网

PHP 翻页 实例代码

[复制链接]
authicon dly 发表于 2011-9-3 19:35:21 | 显示全部楼层 |阅读模式
代码如下:

  1. <?php
  2. class Page
  3. {
  4. private $pageSize;//每一页多少行
  5. private $absolutePage;//当前页
  6. private $pageCount;//总页码数
  7. private $totalNum;//总行数
  8. private $prePage;//上一页
  9. private $nextPage; //下一页
  10. private $sqlStr;
  11. private $baseUrl; //不带GET参数的URL get传输网址
  12. function __construct($pagesize,$absolutepage,$baseurl,$sqlStr)
  13. {
  14. $this->absolutePage = $absolutepage;
  15. $this->pageSize = $pagesize;
  16. $this->baseUrl = $baseurl;
  17. $this->sqlStr = $sqlStr;
  18. }
  19. //显示主体内容
  20. public function listinfo()
  21. {
  22. $result = DBHelper::ExecuteCommand($this->sqlStr);
  23. // $result = mysql_query($this->sqlStr,$conn);
  24. $this->totalNum = mysql_num_rows($result);//取处结果集的所有行数
  25. $this->pageCount = (int)(($this->totalNum -1)/$this->pageSize)+1; //计算出一共有多少页
  26. if($this->absolutePage == "" || !is_numeric($this->absolutePage))//如果是页面第一次加载那么absolutePage的值一定是空的这时我们认为用户是第一次访问就给他看第一页(当然你如果想给他看第5页也可以)
  27. {
  28. $this->absolutePage = 1;//给他看第一页
  29. }
  30. if($this->absolutePage > $this->pageCount)//如果请求的下一页的页码超过了总页数我们就显示最后一页
  31. {
  32. $this->absolutePage = $this->pageCount;//显示最后一页
  33. }
  34. if ($this->totalNum > 1 && $this->absolutePage > 1) //如果满足显示上一页的条件我们就让上一页的页码变量的值等于当前页(absolutePage)减一
  35. {
  36. $this->prePage = $this->absolutePage -1;
  37. }
  38. if($this->absolutePage >= 1 && $this->absolutePage < $this->pageCount)//在当前页至少是1并且不大于总页数时给下一页变量赋值为当前页加1
  39. {
  40. $this->nextPage = $this->absolutePage + 1;
  41. }
  42. if (mysql_data_seek($result,($this->absolutePage -1) * $this->pageSize))//决定从结果集的哪个位置开始检索数据//决定从结果集的哪个位置开始检索数据 mysql_data_seek指向下一行
  43. {
  44. include("goodsTemplate.php");
  45. for ($i = 0; $i < $this->pageSize; $i++)
  46. {
  47. if(($info = mysql_fetch_array($result)))
  48. {
  49. $name = $info['GoodsName'];
  50. $tupian = $info['GoodsPhopo'];
  51. $id = $info['GoodsId'];
  52. $price = $info['GoodsPrice'];
  53. $url = 'detail.php?id='.$id;
  54. $items = str_replace(array('{name}','{tupian}','{url}','{price}','{id}'),array($name,$tupian,$url,$price,$id),$goodsContent);//str_replace替换两个数组 第二个替换第一个数组,$goodsContent参数是要替换的对象
  55. $cishu++;
  56. echo $items;
  57. if ($cishu % 4 == 0)
  58. {
  59. ?>
  60. </tr><tr>
  61. <?php
  62. }
  63. }
  64. }
  65. }
  66. }
  67. //分页动作函数
  68. public function toPage()
  69. {
  70. include('toPageTemplate.php');
  71. if($this->totalNum > 1 && $this->absolutePage > 1)
  72. {
  73. $this->prePage = $this->absolutePage - 1;
  74. }
  75. if($this->absolutePage >= 1 && $this->absolutePage < $this->pageCount)
  76. {
  77. $this->nextPage = $this->absolutePage + 1;
  78. }
  79. $preUrl = $this->baseUrl."?absolutePage=$this->prePage";
  80. $nextUrl = $this->baseUrl."?absolutePage=$this->nextPage";
  81. $items = str_replace(array('{totalNum}','{absolutePage}','{pageCount}','{preUrl}','{nextUrl}'),array($this->totalNum,$this->absolutePage,$this->pageCount,$preUrl,$nextUrl),$toPage);
  82. echo $items;
  83. }
  84. }
  85. ?>
复制代码








上一篇:PHP 取得瑞年与平年的天数的代码
下一篇:PHP 文章中的远程图片采集到本地的代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-6-17 14:01

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表