新手写的分页类

刚学PHP,写的不好,请大家别笑,用Mysql数据库,我还不知道在类中怎么捕捉错误,然后返回。

代码如下:

  1. <?php
  2. class Pageslice{
  3. var $pageSize;//每页显示数据条数
  4. var $page;//当前页面
  5. var $totalPages;//总页数
  6. var $startNum;//strat
  7. var $dbtable;//要查询的数据表
  8. var $rs;//记录集
  9. function setUnit($n){//设置每页显示数据条数
  10. $this->pageSize=$n;
  11. $this->getTotalPages();
  12. $this->getPage();
  13. }
  14. function bindTable($tb){//绑定表
  15. $this->dbtable = $tb;
  16. $this->setUnit(5);//默认显示5条记录
  17. }
  18. function getTotalPages(){//取得总页数
  19. $tb =$this->dbtable;
  20. $sqlStr = "select * from ".$tb;
  21. $rsStr = mysql_query($sqlStr);
  22. $this->totalPages = ceil(count(mysql_fetch_row($rsStr))/$this->pageSize);
  23. return $this->totalPages;
  24. }
  25. function getPage(){//得到当前页码
  26. if( $_GET['page'] == NULL || abs($_GET['page']) >$this->totalPages ){
  27. $this->page = 1;
  28. }else{
  29. $this->page =$_GET['page'];
  30. }
  31. return $this->page;
  32. }
  33. function getRS(){//取记录集
  34. $this->startNum = ($this->page-1)*$this->pageSize;
  35. $tb = $this->dbtable;
  36. $rs_sql = "select * from ".$tb." order by id DESC limit ". $this->startNum.",".$this->pageSize;
  37. $this->rs = mysql_query($rs_sql);
  38. return $this->rs;
  39. }
  40. function showFlip(){//显示分页控件
  41. $page_string ="&nbsp;&nbsp;总共".$this->totalPages."页,当前第".$this->page."页&nbsp;&nbsp;";
  42. if( $this->page == 1 ){
  43. $page_string.="第一页|上一页|";
  44. }else{
  45. $page_string.= "<a href=?page=1>第一页</a>|<a href=?page=".($this->page-1).">上一页</a>|";
  46. }
  47. if( ($this->page == $this->totalPages) || ($this->totalPages == 0) ){
  48. $page_string.=" 下一页|尾页";
  49. }else{
  50. $page_string.= "<a href=?page=".($this->page+1).">下一页</a>|<a href=?page=".$this->totalPages.">尾页</a>";
  51. }
  52. print $page_string;
  53. }
  54. }
  55. ?>[/php]
  56. 应用,假设已经建立了test表,并且已经连上
  57. [php]$pages = new Pageslice;//建立分页对象
  58. $pages->bindTable('test');//读取test表
  59. //$pages->setUnit(3);//可以重新设置显示条数
  60. $rs = $pages->getRS();
  61. /*
  62. .........其他代码
  63. */
  64. $pages->showFlip()
  65. //然后在需要显示分页的地方调用分页