2009
05-8
05-8
原来写的分页类有问题,经过排查,发现一个变量用错了,修改之
效果,很简单,黑白配,有心的人可以自己修改一下风格,欢迎共享
PHP部分
- < ?php
- /* Page.Class.php
- * Page
- * @link http://www.sunboyu.cn
- * @package OA
- * @version V1.0
- *
- * 2009 05 07 sunboyu@gmail.com
- * Demo
- $page = new Page( 1 , 'v_user' , '*' , '' );
- $rs = $page->__getlist();
- $smarty->assign("page",$page->__getpagelist());
- */
- class Page
- {
- public $count; #结果总数
- public $page; #当前页
- public $pagesize; #每页结果数
- public $pagecount; #翻页数
- public $baseurl; #url
- public $result; #结果数组集
- public $pagelist; #每翻页数
- public $db; #数据库连接
- public $table; #要查询的表
- public $fileds; #要返回的字段
- public $where; #where条件
- #构造函数,初始化变量
- function __construct( $page , $table , $fields = '*' , $where = false , $baseurl = false )
- {
- global $_CFG,$db;
- $this->db = $db;
- $this->table = $table;
- $this->page = isset($page) ? intval($page) : 1;
- $this->fileds = $fields;
- $this->pagesize = $_CFG['pagesize'];
- $this->baseurl = ($baseurl!=false) ? $baseurl : $this->__geturl();
- $this->pagelist = $_CFG['pagelist'];
- $this->where = $where;
- }
- #获得当前url
- function __geturl()
- {
- parse_str($_SERVER['QUERY_STRING'],$str);
- if(isset($str['page']))
- {
- unset($str['page']);
- }
- return count($str) > 0 ? "?".http_build_query($str) : "?";
- }
- #获得记录集
- function __getlist()
- {
- #获得count记录
- $sql = sprintf("SELECT COUNT(*) AS table_count FROM %s %s",$this->table,(($this->where!=false) ? " WHERE ".$this->where : ''));
- $rs = $this->db->fetch( $sql );
- $this->count = $rs['table_count'];
- $offset = ($this->page-1)*$this->pagesize-1;
- $sql = sprintf("SELECT %s FROM %s %s LIMIT %d,%d",$this->fileds,$this->table,(($this->where!=false) ? " WHERE ".$this->where : ''),$this->pagesize*($this->page-1),$this->pagesize);
- #echo $sql;
- $list = $this->db->fetchAll( $sql );
- return $list;
- }
- #获得分页列表
- function __getpagelist()
- {
- $this->result['count'] = $this->count;
- $this->result['page'] = $this->page;
- $this->result['pagesize'] = $this->pagesize;
- $this->result['pagecount'] = ceil($this->count/$this->pagesize);
- if($this->result['pagecount']< =1) //只有一页以下
- {
- $this->result['pagelist'] = 0;
- }
- else //一页以上
- {
- #前一页,第一页的算法
- $this->result['first'] = ($this->page == 1) ? 0 : 1;
- $this->result['pre'] = ($this->page == 1) ? 0 : 1;
- #后一页,最后一页的算法
- $this->result['next'] = ($this->page == $this->result['pagecount'] ) ? 0 : 1;
- $this->result['last'] = ($this->page == $this->result['pagecount'] ) ? 0 : 1;
- #起始
- $pagearray = array();
- $start = floor(($this->page-1)/10)*10+1;
- for($i=0;$i<10;$i++)
- {
- if( ($start+$i) < = $this->result['pagecount'])
- {
- $pagearray[$i]['page'] = $start+$i;
- }
- if( ($start+$i) != $this->page )
- {
- $pagearray[$i]['link'] = 1;
- }
- }
- #分页导航列表
- $this->result['pagelist'] = $pagearray;
- $this->result['baseurl'] = $this->baseurl;
- }
- return $this->result;
- }
- }
- ?>
模板部分
- <div id="page">
- <table>
- <tr>
- <td>
- 共{{$page.count}}条数据 每页{{$page.pagesize}}条 共{{$page.pagecount}}页 当前第{{$page.page}}页
- </td>
- <td>
- {{if $page.pagecount>1}}
- {{if $page.first eq 1}}
- <a href="{{$page.baseurl}}">首页</a>
- {{else}}
- 首页
- {{/if}}
- {{if $page.pre eq 1}}
- <a href="{{$page.baseurl}}&page={{$page.page-1}}">上一页</a>
- {{else}}
- 上一页
- {{/if}}
- {{foreach from=$page.pagelist item=vols}}
- {{if ($vols.page > 0 ) && ($vols.page < = $page.pagecount) }}
- {{if $vols.link eq 1}}
- <a href="{{$page.baseurl}}&page={{$vols.page}}">[{{$vols.page}}]
- {{else}}
- [{{$vols.page}}]
- {{/if}}
- {{/if}}
- {{/foreach}}
- {{if $page.next eq 1}}
- <a href="{{$page.baseurl|default:"?"}}&page={{$page.page+1}}">下一页</a>
- {{else}}
- 下一页
- {{/if}}
- {{if $page.last eq 1}}
- <a href="{{$page.baseurl|default:"?"}}&page={{$page.pagecount}}">尾页</a>
- {{else}}
- 尾页
- {{/if}}
- {{/if}}
- </td>
- </tr>
- </table>
- </div>
补充一个demo
- #PHP部分
- $page = new Page( $page , $this->area_table , $fields = '*' , $where);
- $result['rs'] = $page->__getlist();
- $result['page'] = $page->__getpagelist();
- $smarty->assign("list",$result['rs']);
- $smarty->assign("page",$result['page']);
- $smarty->display('list.tpl');
- #模板里边只要引用这个分页模板即可
- {{include file=$smarty.const.Tpl|cat:"/Page.tpl"}}

五月 8th, 2009 at 04:13:21
下一版优化一下,翻页不是那么人性化,本来想抄discuz的,但感觉不好,还是模仿老外的。
五月 15th, 2009 at 11:09:35
朋友,你很强呀,可以交个朋友吗?请教你的QQ号
五月 15th, 2009 at 16:48:00
我的号就在右边
六月 7th, 2009 at 22:44:55
我咋看不懂呢!!
为什么啊!!
能给个实例不???
谢谢了
六月 8th, 2009 at 09:56:50
看了几天了!!
好像有点领悟了!!
汗啊!
二月 8th, 2010 at 21:24:46
有人说:”
2。使用分页类库,呵呵,这个网上就太多了,一大把,不过我还没有发现写得很好的,特别是容易扩展的。
在搜索的时候还看到一个号称分页类终结者的,哈哈,有点好笑。分页类中把SQL都包含进去了,这个是绝对不能容忍的,可以说作者对OO的认识还比较浅。
你怎么看呢?
二月 12th, 2010 at 15:53:43
任何时候,照搬都是不可靠的。但从垃圾中汲取精华是每个程序员必备的功底。
四月 14th, 2010 at 15:29:38
哎呀,真够烂的,都没有整理好就照搬过来