2013年4月1日星期一

【转】细说PHP中smarty分页例子显示优化

这个可能要结合书上的过程来看比较好理解

主要是显示方面!书中例子只有第一页,上一页,下一页的链接,我添加了在上下中间显示五个快速跳转链接如下图

代码如下:

<p align="center">
        共<b><{$pageInfo.row_total}></b>条记录,
        显示第<b><{$pageInfo.page_start}></b>-<b><{$pageInfo.page_end}></b> 条记录
        <a href='<{$url}>?p=1'>首页</a>
        <{if $pageInfo.prev_page}>
            <a href='<{$url}>?p=<{$pageInfo.prev__num}>'>上一页</a>
        <{else}>
            上一页
        <{/if}>
        <{if $pageInfo.page_num <= 6}>
            <{section name=i loop=$pageInfo.page_num}>
                <{if $current_page eq $smarty.section.i.iteration}>
                          <{$smarty.section.i.iteration}>
                  <{else}>
                        <a href ="{$url}?p=<{$smarty.section.i.iteration}>"><{$smarty.section.i.iteration}></a>  
                <{/if}>
            <{/section}>
        <{else}>
            <{if $current_page < 3}>
                <{section name=i loop=5}>
                    <{if $current_page eq $smarty.section.i.iteration}>
                              <{$smarty.section.i.iteration}>
                      <{else}>
                            <a href ="<{$url}>?p=<{$smarty.section.i.iteration}>"><{$smarty.section.i.iteration}></a>  
                    <{/if}>
                <{/section}>
            <{elseif $current_page > $pageInfo.page_num - 3}>
                <{section name=i start=$pageInfo.page_num-5 loop=$pageInfo.page_num+1}>
                    <{if $current_page eq $smarty.section.i.index}>
                              <{$smarty.section.i.index}>
                      <{else}>
                            <a href ="<{$url}>?p=<{$smarty.section.i.index}>"><{$smarty.section.i.index}></a>  
                    <{/if}>
                <{/section}>
            <{else}>
                <{section name=i start=$current_page-2 loop=$current_page+3 show=true}>
                    <{if $current_page eq $smarty.section.i.index}>
                              <{$smarty.section.i.index}>
                      <{else}>
                            <a href ="<{$url}>?p=<{$smarty.section.i.index}>"><{$smarty.section.i.index}></a>  
                    <{/if}>
                <{/section}>
            <{/if}>    
        <{/if}>    
        <{if $pageInfo.next_page}>
            <a href='<{$url}>?p=<{$pageInfo.next_page}>'>下一页</a>
        <{else}>
            下一页
        <{/if}>
        <a href='<{$url}>?p=<{$pageInfo.page_num}>'>最后一页</a>                
        当前<b><{$pageInfo.current_page}>/<{$pageInfo.page_num}></b>页
        </p>

附上我从书上一字一字码下来分页类,(很辛苦的~)

<?php 
    class Page{
        private $total;
        private $page;
        private $num;
        private $pageNum;
        private $offset;
        function __construct($total, $page=1, $num=5){
            $this->total=$total;
            $this->page=$page;
            $this->num=$num;
            $this->pageNum=$this->getPageNum();
            $this->offset=$this->getOffset();
        }
        private function getPageNum(){
            return ceil($this->total/$this->num);
        }
        private function getNextPage(){
            if ($this->page==$this->pageNum)
                return false;
            else 
                return $this->page+1;    
        }
        private function getPrevPage(){
            if ($this->page==1)
                return false;
            else 
                return $this->page-1;    
        }
        private function getOffset(){
            return ($this->page-1)*$this->num;
        }
        private function getStartNum(){
            if ($this->total==0)
                return 0;
            else 
                return $this->offset+1;    
        }
        private function getEndNum(){
            return min($this->offset+$this->num,$this->total);
        }
        public function getPageInfo(){
            $pageInfo=array(
                "row_total"    =>$this->total,
                "row_num"      =>$this->num,
                "page_num"     =>$this->getPageNum(),
                "current_page" =>$this->page,
                "row_offset"   =>$this->getOffset(),
                "next_page"    =>$this->getNextPage(),
                "prev_page"    =>$this->getPrevPage(),
                "page_start"   =>$this->getStartNum(),
                "page_end"     =>$this->getEndNum()
            );
            return $pageInfo;
        }
    }
//end of page_class

总感觉显示的部分代码太多,肯定有办法写到类中的吧,等待发掘了~


摘自:http://hi.baidu.com/czm080/item/beb8a00d0ddd893bf2eafc62