Codeigniter v4 分页库
Codeigniter v4 pagination Library
我在 github 上用这个 link https://github.com/KejawenLab/CI4Pager 找到了 Codeigniter 4 的分页库。我遵循了每一步,但是我在显示数据时遇到了问题,即使我提供了每页显示的数量限制,我也得到了所有数据。但是对于分页 links 它工作正常。请帮忙,我怎样才能限制结果的数量。
这是我的代码
//控制器
public function index()
{
helper('paginator');
$db = \Config\Database::connect();
$result = $db->query('SELECT * FROM users');
$paginator = \App\Libraries\Paginator::createFromResult($result, 1, 1);
return view('page', ['paginator'=> $paginator]);
}
//查看
<?php foreach($paginator->getResults() as $row):?>
<div><?= $row['username'] ?></div>
<?php endforeach;?>
<?= ci_pager($paginator, [
'base_url' => '/vyombo',
'current_text' => 'Current Page',
'total_text' => 'Total Records',
]);
?>
不需要外部库。 Codeigniter 4 支持开箱即用的分页。
您可以对查询结果进行分页,也可以手动对数组进行分页。
https://codeigniter.com/user_guide/libraries/pagination.html?highlight=pagination
万一您在控制器中进行查询,这远非理想。在您的情况下,您想创建一个模型来处理该问题,然后使用该模型。
// In the Controller
public function index()
{
$userModel = new \App\Models\UserModel();
$pageModel = new \App\Models\PageModel();
$data = [
'users' => $userModel->paginate(10, 'group1'),
'pages' => $pageModel->paginate(15, 'group2'),
'pager' => $userModel->pager
];
echo view('users/index', $data);
}
那么在您看来,您可以获得完整链接或简单链接。
// In the views:
<?= $pager->links('group1') ?>
<?= $pager->simpleLinks('group2') ?>
如果您仍想手动对查询进行分页,您可以这样做:
假设您有一个名为 page=1 的查询字符串。然后每页需要 10 个元素。
只需像这样查询:
$limit = 10;
$offset = 10 * ($_GET['page'] - 1);
SELECT *
FROM articles
LIMIT $limit, $offset;
然后要在您的视图中打印分页链接,您只需使用:
<?= $pager->makeLinks($_GET['page'], $limit, $total) ?>
所以对于您的分页,您总是需要两个查询。一个给你当前页面,另一个给你总结果。
通过创建附加视图 table 来解决具有多个 table 的查询
SQL语句“”CREATE OR REPLACE VIEW view_province_incomes AS”,并将table 属性设置为受保护的模型函数$table = 'default_table' ; 但替换 table 内部方法。例如“$this->table = 'view_province_incomes'”,view_province_incomes 自动添加到您的数据库并保存您的命令数据。
//型号
public function getData()
{
//reset table to be used as default
$this->table = 'view_province_incomes';
//creating view table to your database to hold the data
$sql = "CREATE OR REPLACE VIEW view_province_incomes AS ";
$sql .= "
SELECT Date,
SUM(Income) as Income,
SUM(Expense) as Expense,
SUM(Income) - SUM(Expense) as Profit
FROM (
SELECT
province_income.date as Date,
province_income.amount as Income,
0 as Expense
FROM province_income
LEFT JOIN province ON province.provId = province_income.provId
WHERE province_income.provId = 5
UNION ALL
SELECT
province_expenses.date as Date,
0 as Income,
province_expenses.amount as Expense
FROM province_expenses
LEFT JOIN province ON province.provId = province_expenses.provId
WHERE province_expenses.provId = 5
) as t
GROUP BY Date
";
$this->db->query($sql);
return $this; //to make easy chaining with the instance
}
//控制器
public function index()
{
$data['results'] = $this->model->getData()->asObject()->paginate(4);
$data['pager'] = $this->model->pager->links();
return view('Income/index', $data);
}
// 观看次数
<?php foreach ($results as $result): ?>
<div style="display: flex; justify-content: space-between;">
<p> <?= $result->Date ?></p>
<p> <?= $result->Income ?></p>
<p> <?= $result->Expense ?></p>
<p> <?= $result->Profit ?></p>
</div>
<?php endforeach;?>
<!-- pagination links -->
<?= $pager ?>
// 输出
+------------+-----------------------------+
| Date | Incomes | Expense | Profit |
+------------+----------+---------+--------+
| 2018-06-12 | 10000 | 5000 | 5000 |
+------------+----------+---------+--------+
| 2018-06-14 | 17000 | 9000 | 8000 |
+------------+----------+---------+--------+
| 2018-06-15 | 8000 | 6000 | 2000 |
+------------------------------------------+
我在 github 上用这个 link https://github.com/KejawenLab/CI4Pager 找到了 Codeigniter 4 的分页库。我遵循了每一步,但是我在显示数据时遇到了问题,即使我提供了每页显示的数量限制,我也得到了所有数据。但是对于分页 links 它工作正常。请帮忙,我怎样才能限制结果的数量。
这是我的代码
//控制器
public function index()
{
helper('paginator');
$db = \Config\Database::connect();
$result = $db->query('SELECT * FROM users');
$paginator = \App\Libraries\Paginator::createFromResult($result, 1, 1);
return view('page', ['paginator'=> $paginator]);
}
//查看
<?php foreach($paginator->getResults() as $row):?>
<div><?= $row['username'] ?></div>
<?php endforeach;?>
<?= ci_pager($paginator, [
'base_url' => '/vyombo',
'current_text' => 'Current Page',
'total_text' => 'Total Records',
]);
?>
不需要外部库。 Codeigniter 4 支持开箱即用的分页。
您可以对查询结果进行分页,也可以手动对数组进行分页。
https://codeigniter.com/user_guide/libraries/pagination.html?highlight=pagination
万一您在控制器中进行查询,这远非理想。在您的情况下,您想创建一个模型来处理该问题,然后使用该模型。
// In the Controller
public function index()
{
$userModel = new \App\Models\UserModel();
$pageModel = new \App\Models\PageModel();
$data = [
'users' => $userModel->paginate(10, 'group1'),
'pages' => $pageModel->paginate(15, 'group2'),
'pager' => $userModel->pager
];
echo view('users/index', $data);
}
那么在您看来,您可以获得完整链接或简单链接。
// In the views:
<?= $pager->links('group1') ?>
<?= $pager->simpleLinks('group2') ?>
如果您仍想手动对查询进行分页,您可以这样做:
假设您有一个名为 page=1 的查询字符串。然后每页需要 10 个元素。
只需像这样查询:
$limit = 10;
$offset = 10 * ($_GET['page'] - 1);
SELECT *
FROM articles
LIMIT $limit, $offset;
然后要在您的视图中打印分页链接,您只需使用:
<?= $pager->makeLinks($_GET['page'], $limit, $total) ?>
所以对于您的分页,您总是需要两个查询。一个给你当前页面,另一个给你总结果。
通过创建附加视图 table 来解决具有多个 table 的查询 SQL语句“”CREATE OR REPLACE VIEW view_province_incomes AS”,并将table 属性设置为受保护的模型函数$table = 'default_table' ; 但替换 table 内部方法。例如“$this->table = 'view_province_incomes'”,view_province_incomes 自动添加到您的数据库并保存您的命令数据。
//型号
public function getData()
{
//reset table to be used as default
$this->table = 'view_province_incomes';
//creating view table to your database to hold the data
$sql = "CREATE OR REPLACE VIEW view_province_incomes AS ";
$sql .= "
SELECT Date,
SUM(Income) as Income,
SUM(Expense) as Expense,
SUM(Income) - SUM(Expense) as Profit
FROM (
SELECT
province_income.date as Date,
province_income.amount as Income,
0 as Expense
FROM province_income
LEFT JOIN province ON province.provId = province_income.provId
WHERE province_income.provId = 5
UNION ALL
SELECT
province_expenses.date as Date,
0 as Income,
province_expenses.amount as Expense
FROM province_expenses
LEFT JOIN province ON province.provId = province_expenses.provId
WHERE province_expenses.provId = 5
) as t
GROUP BY Date
";
$this->db->query($sql);
return $this; //to make easy chaining with the instance
}
//控制器
public function index()
{
$data['results'] = $this->model->getData()->asObject()->paginate(4);
$data['pager'] = $this->model->pager->links();
return view('Income/index', $data);
}
// 观看次数
<?php foreach ($results as $result): ?>
<div style="display: flex; justify-content: space-between;">
<p> <?= $result->Date ?></p>
<p> <?= $result->Income ?></p>
<p> <?= $result->Expense ?></p>
<p> <?= $result->Profit ?></p>
</div>
<?php endforeach;?>
<!-- pagination links -->
<?= $pager ?>
// 输出
+------------+-----------------------------+
| Date | Incomes | Expense | Profit |
+------------+----------+---------+--------+
| 2018-06-12 | 10000 | 5000 | 5000 |
+------------+----------+---------+--------+
| 2018-06-14 | 17000 | 9000 | 8000 |
+------------+----------+---------+--------+
| 2018-06-15 | 8000 | 6000 | 2000 |
+------------------------------------------+