仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1262|回复: 18
打印 上一主题 下一主题

[学习教程] PHP教程之ADODB和SMARTY联合的分页类

[复制链接]
乐观 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:10:19 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
HTML中的任何元素都要亲自实践,只有明白了什么元素会起到什么效果之后,你才会记忆深刻,而一味的啃书,绝对是不行的,我想大部分新手之所以觉得概念难学,大部分是一个字“懒”,懒是阻止进步的最大敌人,所以克服掉懒的习惯,才能更快的学好一样东西。ado|分页   <?php
/*
+-----------------------------------------------------------------------+
| 作者:一天空一(曾用名:亿时期收集)
| 网址:www.phpwb.com www.esdwl.com
| Q Q: 31769416
| 工夫:2006-05-03                                 
|+-----------------------------------------------------------------------+
*/
    class show_Pager
    {
      protected  $_total;                          //纪录总数
      protected  $pagesize;                       //每页显示的纪录数
      public     $pages;                         //总页数
      protected  $_cur_page;                    //以后页码
      protected  $offset;                      //纪录偏移量
      protected  $pager_Links;                //url毗连
      protected  $pernum = 5;                //页码偏移量,这里可随便更改
   
      public function __construct($total,$pagesize,$_cur_page)
        {   
        $this->_total=$total;
        $this->pagesize=$pagesize;
        $this->_offset();
        $this->_pager();
        $this->cur_page($_cur_page);
        $this->link();
    }
   
    public  function _pager()//盘算总页数
    {
    return $this->pages = ceil($this->_total/$this->pagesize);
    }
   
   
    public function cur_page($_cur_page) //设置页数
    {     
               if (isset($_cur_page))
           {
           $this->_cur_page=intval($_cur_page);
           }
           else
           {
            $this->_cur_page=1; //设置为第一页
           }
        return  $this->_cur_page;
   }
   
//数据库纪录偏移量
   public function _offset()
   {
   return $this->offset=$this->pagesize*($this->_cur_page-1);
   }
   
  //html毗连的标签
  public function link()
  {
    if ($this->_cur_page == 1 && $this->pages>1)
        {
            //第一页
            $this->pager_Links = "首 页 | 上一页 | <a href=".$PHP_SELF."?pager=".($this->_cur_page+1).">下一页</a> | <a href=".$PHP_SELF."?pager=$this->pages>尾 页</a>";
        }
        elseif($this->_cur_page == $this->pages && $this->pages>1)
        {
            //最初一页
             $this->pager_Links = "<a href=".$PHP_SELF."?pager=1>首 页<a> | <a href=".$PHP_SELF."?pager=".($this->_cur_page-1).">上一页</a> | 下一页 | 尾 页";
        }
        elseif ($this->_cur_page > 1 && $this->_cur_page <= $this->pages)
        {
            //两头
             $this->pager_Links = "<a href=".$PHP_SELF."?pager=1>首 页<a> | <a href=".$PHP_SELF."?pager=".($this->_cur_page-1).">上一页</a> | <a href=".$PHP_SELF."?pager=".($this->_cur_page+1).">下一页</a> | <a href=".$PHP_SELF."?pager=$this->pages>尾 页</a>";
        }
        return $this->pager_Links;
  }
  
  //html数字毗连的标签
   public function num_link()
  {
       $setpage  = $this->_cur_page ? ceil($this->_cur_page/$this->pernum) : 1;
        $pagenum   = ($this->pages > $this->pernum) ? $this->pernum : $this->pages;
        if ($this->_total  <= $this->pagesize) {
            $text  = '只要一页';
        } else {
            $text = '页数:'.$this->pages.' '.$this->pagesize.'个/页 ';
            if ($this->_cur_page > 1) {
                $text .= '<a title=第一页 href=?pager=1>[1]</a>..';
            }
            if ($setpage > 1) {
                $lastsetid = ($setpage-1)*$this->pernum;
                $text .= '<a title=上一列 href=?pager='.$lastsetid.'>[<<]</a>';
            }
            if ($this->_cur_page > 1) {
                $pre = $this->_cur_page-1;
                $text .= '<a title=上一页 href=?pager='.$pre.'>[<]</a>';
            }
            $i = ($setpage-1)*$this->pernum;
            for($j=$i; $j<($i+$pagenum) && $j<$this->pages; $j++) {
                $newpage = $j+1;
                if ($this->_cur_page == $j+1) {
                    $text .= '<b>['.($j+1).']</b>';
                } else {
                    $text .= '<a href=?pager='.$newpage.'>['.($j+1).']</a>';
                }
            }            
            if ($this->_cur_page < $this->pages){
                $next = $this->_cur_page+1;
                $text .= '<a title=下一页 href=?pager='.$next.'>[>]</a>';
            }
            if ($setpage < $this->_total) {
                $nextpre = $setpage*($this->pernum+1);
                if($nextpre<$this->pages)
                $text .= '<a title=下一列 href=?pager='.$nextpre.'>[>>]</a>';
            }
            if ($this->_cur_page < $this->pages) {
                $text .= '..<a title=最初一页 href=?pager='.$this->pages.'>['.$this->pages.']</a>';
            }
         }
            return $text;
         }
    }
//$conn,$tpl 全局变量 传入 $table是数据表 $where 是前提语句 $order 是ADSC之类的,$pager_size是每页显示数,$pager是以后页
//前往后在摸板外面可以直接利用
<{section name=sec loop=$show}>  <{/section}>
数字标签用<{$numlink}>
    class my_Pager extends show_Pager
    {
        function __construct($table,$where,$order,$pager_size,$pager)
        {
         global $conn;
         global $tpl;
         $sql="SELECT * FROM `$table` $where order by $order desc";
         $nums=$conn->Execute($sql)->RecordCount();
         $pager=new show_Pager($nums,$pager_size,$pager);
         $show=$conn->SelectLimit($sql,$pager_size,$pager->_offset())->GetAll();
         $tpl->assign("numlink",$pager->num_link());//数字标签
         $tpl->assign("show",$show);//显示帖子
        }
    }
?>对于PHP的语法结构,刚开始真的很不习惯,真搞不懂为什么每个变量之前都要加个“$”符号,每个语句写完之后都必须加上“分号”来表示此句已经结束,还有,PHP对字母的大小写是敏感的,写的时候一定要注意大小写的区别。
乐观 该用户已被删除
沙发
 楼主| 发表于 2015-3-12 22:58:39 | 显示全部楼层
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-22 07:37

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表