medoo:一个通用的php的数据库客户端。
"pagerfanta/pagerfanta":"1.0.5" 一个通用的php的分页组件。
结合在一起棒棒哒!
首先,composer安装
"pagerfanta/pagerfanta":"1.0.5"
"catfan/medoo":"1.4.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
请自行插入一百条数据
分页代码
'mysql', 'database_name' => 'test1', 'server' => '127.0.0.1', 'username' => 'root', 'password' => 'root', // 可选,但最好加上 'charset' => 'utf8mb4', 'port' => 3306, // [optional] Enable logging (Logging is disabled by default for better performance) 'logging' => true, // [optional] driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php 'option' => [ \PDO::ATTR_STRINGIFY_FETCHES => false, \PDO::ATTR_EMULATE_PREPARES => false ] ] ); //构造查询条件 $table = 'test_databases'; $where=["id[<]"=>90]; $col='*'; //我把两个类一起放这个文件,工程中最好分开 // 注,medoo还有带join的select方法,建议再写一个适配器,然后根据查询条件的不同 // 调用不同的适配器,反正也就两个。 $adapter = new MedooPageAdapter($database,$table,$col, $where); $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']."
"; } //打印分页链接。 $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 "" .$html.""; return $res; } private function default_css() { $css=<<.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; } css; return $css; } } /** * 分页适配器 * @author xieye * */ class MedooPageAdapter implements AdapterInterface { private $table=''; private $where=[]; private $db ; private $col; public function __construct($db,$table, $col, $where){ $this->table = $table; $this->where = $where; $this->col = $col; $this->db = $db; } public function getNbResults(){ $db = $this->db; return $db->count($this->table, $this->where); } /** * Returns an slice of the results. * * @param integer $offset The offset. * @param integer $length The length. * * @return array|\Traversable The slice. */ public function getSlice($offset, $length){ $where = $this->where; $where["LIMIT"] = [$offset, $length]; return $this->db->select($this->table,$this->col, $where); } }
浏览器效果