• fleaphp常用方法分页之Pager


    Pager 分页函数

    1. /**  
    2.      * 构造函数  
    3.      *  
    4.      * 如果 $source 参数是一个 TableDataGateway 对象,则 FLEA_Helper_Pager 会调用  
    5.      * 该 TDG 对象的 findCount() 和 findAll() 来确定记录总数并返回记录集。  
    6.      *  
    7.      * 如果 $source 参数是一个字符串,则假定为 SQL 语句。这时,FLEA_Helper_Pager  
    8.      * 不会自动调用计算各项分页参数。必须通过 setCount() 方法来设置作为分页计算  
    9.      * 基础的记录总数。  
    10.      *  
    11.      * 同时,如果 $source 参数为一个字符串,则不需要 $conditions 和 $sortby 参数。  
    12.      * 而且可以通过 setDBO() 方法设置要使用的数据库访问对象。否则 FLEA_Helper_Pager  
    13.      * 将尝试获取一个默认的数据库访问对象。  
    14.      *  
    15.      * @param TableDataGateway|string $source  
    16.      * @param int $currentPage  
    17.      * @param int $pageSize  
    18.      * @param mixed $conditions  
    19.      * @param string $sortby  
    20.      * @param int $basePageIndex  
    21.      *  
    22.      * @return FLEA_Helper_Pager  
    23.      */  
    24. function FLEA_Helper_Pager(& $source$currentPage$pageSize = 20, $conditions = null, $sortby = null, $basePageIndex = 0)   
    25.     {   
    26.         $this->_basePageIndex = $basePageIndex;   
    27.         $this->_currentPage = $this->currentPage = $currentPage;   
    28.         $this->pageSize = $pageSize;   
    29.   
    30.         if (is_object($source)) {   
    31.             $this->source =& $source;   
    32.             $this->_conditions = $conditions;   
    33.             $this->_sortby = $sortby;   
    34.             $this->totalCount = $this->count = (int)$this->source->findCount($conditions);   
    35.             $this->computingPage();   
    36.         } elseif (!emptyempty($source)) {   
    37.             $this->source = $source;   
    38.             $sql = "SELECT COUNT(*) FROM ( $source ) as _count_table";   
    39.             $this->dbo =& FLEA::getDBO();   
    40.             $this->totalCount = $this->count = (int)$this->dbo->getOne($sql);   
    41.             $this->computingPage();   
    42.         }   
    43.     }  

    Pager 参数说明
    $source 数据库操作类
    $currentPage 当前页
    $pageSize 每页显示记录数量
    $conditions 查询条件
    $sortby 排序方式
    $basePageIndex 页码基数

    实例:

    1. $dirname = dirname(__FILE__);   
    2. define('APP_DIR'$dirname . '/APP');   
    3. define('NO_LEGACY_FLEAPHP', true);   
    4. require($dirname.'/FleaPHP/FLEA/FLEA.php');   
    5.   
    6. //设置缓存目录   
    7. FLEA::setAppInf('internalCacheDir',$dirname.'/_Cache');   
    8.   
    9. //链接数据库   
    10.   
    11. $dsn = array(   
    12.         'driver'    => 'mysql',   
    13.         'host'      => 'localhost',   
    14.         'login'     => 'root',   
    15.         'password'  => '',   
    16.         'database'  => 'wordpress'  
    17. );   
    18.   
    19. FLEA::setAppInf('dbDSN',$dsn);   
    20.   
    21. //读取wp_posts的内容   
    22. FLEA::loadClass('FLEA_Db_TableDataGateway');   
    23. FLEA::loadClass('FLEA_Helper_Pager');
    24. $page_size='10';       //每页记录数
         $page = (isset($_GET['page']))?(int)$_GET['page']:1;
         $conditions = array(
          'ss_state'=>0,
         );
         $pager = new FLEA_Helper_Pager($this->_shuoshuo,$page,$page_size,$conditions,'created DESC',1);
         $rows =$pager->findAll();
         $pager->setBasePageIndex(1);  // 起始页码设为1
         $this->smarty->assign('rowset',$rows);
         $this->smarty->assign('newsNavbar', $pager->getNavbarIndexs($page, 8));
         $this->smarty->assign('newsPager', $pager->getPagerData());
         $url = array('ctl'=>'default','act'=>'helpinfo&act=shuoshuo');
         $this->smarty->assign('url',$url);

     

    <a href="{{url controller=$url.ctl action=$url.act page=$newsPager.firstPageNumber}}" title='首页'><<</a>{{section name=page loop=$newsNavbar}}{{if $newsNavbar[page].index == $newsPager.currentPage}}<b><font color='red'>[{{$newsNavbar[page].number}}]</font></b>{{else}}<a href="{{url controller=$url.ctl action=$url.act page=$newsNavbar[page].index}}">{{$newsNavbar[page].number}}</a>{{/if}}{{/section}}<a href="{{url controller=$url.ctl action=$url.act page=$newsPager.lastPageNumber}}" title='末页'>>></a><!--    共有 <font color="Red">{{$newsPager.count}}</font> 条记录,分为 <font color="Red">{{$newsPager.pageCount}}</font> 页显示,每页 <font color="Red">{{$newsPager.pageSize}}</font> 条-->

  • 相关阅读:
    unity3d进程通信利用WM_COPYDATE和HOOK
    java解析XML saxReader.read(xml) 错误:org.dom4j.DocumentException: no protocol
    Linux下永久改动MAC地址和ifconfig命令总结
    Android-Universal-Image-Loader学习笔记(3)--内存缓存
    图片3d轮放查看效果
    Hadoop-eclipse-plugin插件安装
    Hadoop格式化HDFS报错java.net.UnknownHostException: localhost.localdomain: localhost.localdomain
    linux出现bash: ./java: cannot execute binary file 问题的解决办法
    分析windows宿主机Ping不通linux虚拟机的其中一种情况
    http://jingyan.baidu.com/article/08b6a591f0fafc14a9092275.html
  • 原文地址:https://www.cnblogs.com/tdalcn/p/2025592.html
Copyright © 2020-2023  润新知