php利用Pagerfanta给通用数据库类库Medoo分页

本文介绍如何使用Medoo和Pagerfanta两个PHP类库进行数据库操作和分页功能实现。通过Composer安装所需依赖,创建数据库表并插入数据,演示了如何结合这两个库完成高效的分页展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是两个非常优秀的php类库

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


请自行插入一百条数据

分页代码

<?php
namespace app\control;
use Medoo\Medoo as me;

use Pagerfanta\Pagerfanta;
use Pagerfanta\View\DefaultView;
use Pagerfanta\Adapter\AdapterInterface;

class MedooPage {
public function index($req, $res, $args) {
$database = new me ( [
// 必须
'database_type' => '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']."<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;
}
}

/**
* 分页适配器
* @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);
}

}


浏览器效果
[img]http://dl2.iteye.com/upload/attachment/0125/9393/328323c6-aa74-3c62-a046-670157d2521c.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值