We just added a method to get a single page of article records which we're calling here in the index
page.
We can restrict the number of records we display by passing in values for the limit and the offset it
would be easier to specify a page number instead of these two values instead of doing this in the method
in the article class though.
Let's add a new class that we can reuse to paginate different database tables if we need to.
We'll put it in the classis folder and we'll call it paginated.
Doug P. HP
first will add the class definition then will add properties for the limit and offset.
Next let's add a constructor method where we can calculate the limits and offset based on the page number.
The limit will simply be the number of rijkaard per page.
The offset for the first page will be zero.
Starting from the first rijkaard.
If we have three Ricardos per page for example the offset for the second page will be 3 for the third
page 6 and so on.
So we can calculate the offset using this formula.
The number of Rickard's per page multiplied by the page number minus one.
So we'll pass the page number and the number of Ricardos into the constructor.
Then the limit property will simply be the number of records and the offset property is calculated using
that formula.
It may seem unnecessary to just pass the records per page value in just to return this same value as
the limit property but I will use this value later on when we calculate page links before we use this
class.
Let's add a p p duck comment for the constructor.
The two properties and the class itself.
Then in the index page we don't need to require the new class as the auto loader will take care of that.
Let's create a new paginate up ject passing in a page number of 1 and 4 for the number of ecards per
page.
Then when we call the Get page method we can pass in the properties from the paginate an object like
that.
Let's give that a try with a page number of 1 and four records per page.
We see the first four records.
If I change the page to two
we see the second page.
If I change it to three
we see the third page and so on.