新手写的分页类
时间:2014-06-09 16:40来源: 作者: 点击:
次
刚学PHP,写的不好,请大家别笑,用Mysql数据库,我还不知道在类中怎么捕捉错误,然后返回。 代码如下: ?php class Pageslice{ var $pageSize ; //每页显示数据条数 var $page ; //当前页面 var $totalPages ; //总
刚学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()
-
(责任编辑:admin) |
------分隔线----------------------------