新手写的分页类
刚学PHP,写的不好,请大家别笑,用Mysql数据库,我还不知道在类中怎么捕捉错误,然后返回。
代码如下:
- <?php
- class Pageslice{
- var $pageSize;
- var $page;
- var $totalPages;
- var $startNum;
- var $dbtable;
- var $rs;
-
-
- function setUnit($n){
- $this->pageSize=$n;
- $this->getTotalPages();
- $this->getPage();
- }
-
-
- function bindTable($tb){
- $this->dbtable = $tb;
- $this->setUnit(5);
- }
-
-
- function getTotalPages(){
- $tb =$this->dbtable;
- $sqlStr = "select * from ".$tb;
- $rsStr = mysql_query($sqlStr);
- $this->totalPages = ceil(count(mysql_fetch_row($rsStr))/$this->pageSize);
- return $this->totalPages;
- }
-
-
- function getPage(){
- if( $_GET['page'] == NULL || abs($_GET['page']) >$this->totalPages ){
- $this->page = 1;
- }else{
- $this->page =$_GET['page'];
- }
- return $this->page;
- }
-
-
- function getRS(){
- $this->startNum = ($this->page-1)*$this->pageSize;
- $tb = $this->dbtable;
- $rs_sql = "select * from ".$tb." order by id DESC limit ". $this->startNum.",".$this->pageSize;
- $this->rs = mysql_query($rs_sql);
- return $this->rs;
- }
-
-
- function showFlip(){
- $page_string =" 总共".$this->totalPages."页,当前第".$this->page."页 ";
- if( $this->page == 1 ){
- $page_string.="第一页|上一页|";
- }else{
- $page_string.= "<a href=?page=1>第一页</a>|<a href=?page=".($this->page-1).">上一页</a>|";
- }
- if( ($this->page == $this->totalPages) || ($this->totalPages == 0) ){
- $page_string.=" 下一页|尾页";
- }else{
- $page_string.= "<a href=?page=".($this->page+1).">下一页</a>|<a href=?page=".$this->totalPages.">尾页</a>";
- }
- print $page_string;
- }
-
-
- }
- ?>[/php]
-
- 应用,假设已经建立了test表,并且已经连上
- [php]$pages = new Pageslice;
- $pages->bindTable('test');
-
- $rs = $pages->getRS();
-
-
- $pages->showFlip()
-