Skip to content

Commit af4271f

Browse files
authored
Merge pull request #6181 from kenjis/fix-docs-pagination.rst
docs: improve pagination.rst
2 parents fdc4e92 + c8d2fb9 commit af4271f

3 files changed

Lines changed: 52 additions & 19 deletions

File tree

user_guide_src/source/libraries/pagination.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ to load it manually:
1818

1919
.. literalinclude:: pagination/001.php
2020

21-
***************************
22-
Paginating Database Results
23-
***************************
21+
.. _paginating-with-models:
22+
23+
**********************
24+
Paginating with Models
25+
**********************
2426

2527
In most cases, you will be using the Pager library in order to paginate results that you retrieve from the database.
2628
When using the :doc:`Model </models/model>` class, you can use its built-in ``paginate()`` method to automatically
@@ -37,8 +39,8 @@ The first element is the results from the database, **users**, which is retrieve
3739
the Model will hold on to the instance it used and store it in the public property, ``$pager``. So, we grab
3840
that and assign it to the ``$pager`` variable in the view.
3941

40-
.. important:: It is important to understand that the Model::paginate() method uses the Model and QueryBuilder methods.
41-
Therefore, trying to use ``$db->query()`` and Model::paginate() **will not work** because ``$db->query()`` executes
42+
.. important:: It is important to understand that the ``Model::paginate()`` method uses the **Model** and **QueryBuilder** methods.
43+
Therefore, trying to use ``$db->query()`` and ``Model::paginate()`` **will not work** because ``$db->query()`` executes
4244
the query immediately and is not associated with a QueryBuilder.
4345

4446
To define conditions for pagination in a model, you can:
@@ -91,29 +93,26 @@ like **https://domain.tld/foo/bar/[pageNumber]** instead of **https://domain.tld
9193

9294
Please note: ``$segment`` value cannot be greater than the number of URI segments plus 1.
9395

96+
*****************
9497
Manual Pagination
95-
=================
98+
*****************
9699

97100
You may find times where you just need to create pagination based on known data. You can create links manually
98101
with the ``makeLinks()`` method, which takes the current page, the number of results per page, and
99-
the total number of items as the first, second, and third parameters, respectively::
102+
the total number of items as the first, second, and third parameters, respectively:
100103

101-
<?= $pager->makeLinks($page, $perPage, $total) ?>
104+
.. literalinclude:: pagination/015.php
102105

103106
This will, by default, display the links in the normal manner, as a series of links, but you can change the display
104107
template used by passing in the name of the template as the fourth parameter. More details can be found in the following
105-
sections.
108+
sections::
106109

107-
::
108-
109-
<?= $pager->makeLinks($page, $perPage, $total, 'template_name') ?>
110+
$pager->makeLinks($page, $perPage, $total, 'template_name');
110111

111112
It is also possible to use a URI segment for the page number, instead of the page query parameter, as described in
112-
the previous section. Specify the segment number to use as the fifth parameter to ``makeLinks()``.
113-
114-
::
113+
the previous section. Specify the segment number to use as the fifth parameter to ``makeLinks()``::
115114

116-
<?= $pager->makeLinks($page, $perPage, $total, 'template_name', $segment) ?>
115+
$pager->makeLinks($page, $perPage, $total, 'template_name', $segment);
117116

118117
Please note: ``$segment`` value cannot be greater than the number of URI segments plus 1.
119118

@@ -123,8 +122,9 @@ If you in need to show many pagers on one page then additional parameter which w
123122

124123
Pagination library uses **page** query parameter for HTTP queries by default (if no group or ``default`` group name given) or ``page_[groupName]`` for custom group names.
125124

125+
*************************************
126126
Paginating with Only Expected Queries
127-
=====================================
127+
*************************************
128128

129129
By default, all GET queries are shown in the pagination links.
130130

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use CodeIgniter\Controller;
6+
7+
class UserController extends Controller
8+
{
9+
public function index()
10+
{
11+
// ...
12+
13+
$pager = service('pager');
14+
15+
$page = (int) ($this->request->getGet('page') ?? 1);
16+
$perPage = 20;
17+
$total = 200;
18+
19+
// Call makeLinks() to make pagination links.
20+
$pager_links = $pager->makeLinks($page, $perPage, $total);
21+
22+
$data = [
23+
// ...
24+
'pager_links' => $pager_links,
25+
];
26+
27+
return view('users/index', $data);
28+
}
29+
}
30+
?>
31+
32+
<!-- In your view file: -->
33+
<?= $pager_links ?>

user_guide_src/source/models/model.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ CodeIgniter does provide a model class that provides a few nice features, includ
3434
- automatic database connection
3535
- basic CRUD methods
3636
- in-model validation
37-
- automatic pagination
37+
- :ref:`automatic pagination <paginating-with-models>`
3838
- and more
3939

4040
This class provides a solid base from which to build your own models, allowing you to
@@ -116,7 +116,7 @@ The Model's CRUD methods will take a step of work away from you and automaticall
116116
the resulting data, instead of the Result object. This setting allows you to define
117117
the type of data that is returned. Valid values are '**array**' (the default), '**object**', or the **fully
118118
qualified name of a class** that can be used with the Result object's ``getCustomResultObject()``
119-
method. Using the special ``::class`` constant of the class will allow most IDEs to
119+
method. Using the special ``::class`` constant of the class will allow most IDEs to
120120
auto-complete the name and allow functions like refactoring to better understand your code.
121121

122122
$useSoftDeletes

0 commit comments

Comments
 (0)