V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yyy123456
V2EX  ›  问与答

doctrine 分页组件 pagerfanta 单独使用完全攻略

  •  
  •   yyy123456 · 2017-07-06 16:38:22 +08:00 · 1735 次点击
    这是一个创建于 2492 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本文目的是 php 分页显示数据。

    为了独立使用 composer,不依赖框架。

    假设当前使用的 db 类库是 doctrine,则分页该怎么用?

    本代码完全脱离 symfony 环境。只加载对应的 db 类库,故意不使用模板,让代码含义更加清晰。

    composer

    {
        "require": {
            "doctrine/dbal":"2.5.12",
            "pagerfanta/pagerfanta":"1.0.5"
        }
    }
    

    建表

    CREATE TABLE `test_databases` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `db_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '库名',
      `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '测试用户 id',
      `created_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
      `updated_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '修改时间',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB
    

    请自行插入一百条数据。

    假设本机项目域名 www.t3.com

    本代码网址 http://www.t3.com/paginator/doctrine

    首页只需输入上面网址即可,点击分页链接,会自动加 page 查询参数。

    <?php
    namespace app\control;
    use Doctrine\DBAL\Query\QueryBuilder;
    use Doctrine\DBAL\Configuration;
    use Doctrine\DBAL\DriverManager;
    
    use Pagerfanta\Adapter\DoctrineDbalAdapter;
    use Pagerfanta\Pagerfanta;
    use Pagerfanta\View\DefaultView;
    
    class Paginator {
        public function doctrine( $req, $res, $args) {
            $config = new Configuration();
            //..
            $connectionParams = array(
            		'dbname' => 'test1',
            		'user' => 'root',
            		'password' => 'root',
            		'host' => '127.0.0.1',
            		'driver' => 'pdo_mysql',
                    'charset'=>'UTF8',
            );
            $conn = DriverManager::getConnection($connectionParams, $config);
            
            //构造查询语句。
            $queryBuilder = new QueryBuilder($conn);
            $queryBuilder->select('p.*')->where("p.id < 100")->from('test_databases', 'p')
               ->orderBy("p.id","asc");
            
            $countQueryBuilderModifier = function ($queryBuilder) {
                $queryBuilder->select('COUNT(*) AS total_results')
                      ->setMaxResults(1);
            };
            $adapter = new DoctrineDbalAdapter($queryBuilder, $countQueryBuilderModifier);
            $pagerfanta = new Pagerfanta($adapter);
            
            $page = intval( $_GET["page"]);
            if (!$page) {
                $page=1;
            }
            
            //设置当前页,最大页面。
            $pagerfanta->setMaxPerPage(4)->setCurrentPage($page);
            
            //打印当前页面的结果
            foreach ($pagerfanta->getCurrentPageResults() as $v ) {
                echo $v['db_name'] .' = ' . $v['user_id']."<br>";
            }
            
            //打印分页链接。
            $routeGenerator = function($page) { // 匿名函数解决链接字符串
                return '/paginator/doctrine?page='.$page;
            };
            $view = new DefaultView();
            $options = array('proximity' => 3); // 这个数字干嘛用?中间的链接个数=这个数字*2+1,这个数字一般取 3.
            $html = $view->render($pagerfanta, $routeGenerator, $options);
            echo $this->default_css();
            echo  "<div class='pagerfanta'>" .$html."</div>";
            return $res;
        }
        
        private function default_css()
        {
            $css=<<<css
            <style>
            .pagerfanta {
    }
    
    .pagerfanta a,
    .pagerfanta span {
        display: inline-block;
        border: 1px solid blue;
        color: blue;
        margin-right: .2em;
        padding: .25em .35em;
    }
    
    .pagerfanta a {
        text-decoration: none;
    }
    
    .pagerfanta a:hover {
        background: #ccf;
    }
    
    .pagerfanta .dots {
        border-width: 0;
    }
    
    .pagerfanta .current {
        background: #ccf;
        font-weight: bold;
    }
    
    .pagerfanta .disabled {
        border-color: #ccf;
        color: #ccf;
    }
    
    
    
    .pagerfanta a,
    .pagerfanta span {
        border-color: blue;
        color: blue;
    }
    
    .pagerfanta a:hover {
        background: #ccf;
    }
    
    .pagerfanta .current {
        background: #ccf;
    }
    
    .pagerfanta .disabled {
        border-color: #ccf;
        color: #cf;
    }
            </style>
    css;
            return $css;
        }
    }
    

    效果展示

    图片 图片 图片 图片

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2210 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:19 · PVG 17:19 · LAX 02:19 · JFK 05:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.