新手写的分页类
刚学PHP,写的不好,请大家别笑,用Mysql数据库,我还不知道在类中怎么捕捉错误,然后返回。
代码如下:
<?php class Pageslice{ var $pageSize ; //每页显示数据条数 var $page ; //当前页面 var $totalPages ; //总页数 var $startNum ; //strat var $dbtable ; //要查询的数据表 var $rs ; //记录集 function setUnit( $n ){ //设置每页显示数据条数 $this ->pageSize= $n ; $this ->getTotalPages(); $this ->getPage(); } function bindTable( $tb ){ //绑定表 $this ->dbtable = $tb ; $this ->setUnit(5); //默认显示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' ); //读取test表 //$pages->setUnit(3);//可以重新设置显示条数 $rs = $pages ->getRS(); /* .........其他代码 */ $pages ->showFlip() //然后在需要显示分页的地方调用分页声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did29593