Welcome in his coding lecture, I'm going to show you how to create a pagination using stylized expressions.
All right, let's get started.
You know, when you search on Google, it shows your results page, then you move through the pages
or the next results.
It's called pagination.
Now we're going to imitate the same thing using the items that we saw in the previous lecture.
But before that, they removed the previous slices here except the items.
OK, let me show you how the pagination is going to work.
Let's say I want to display four items at a time, so let me print the first page by slicing it up to
the first four elements named Duplicate this three times.
OK, here is the second page.
It is between the fourth index and the eight item.
Here is the third page is between the eight index and the twelfth item.
And here is the last page.
Limerence, as you can see, there are four items per page, right, however.
Pay attention to the last page.
It has only one item.
So we will need to handle this case in the court as well.
For example, this will result in an error.
It is because the 14th element doesn't exist.
OK, let me take it back.
If you compare the pages to all the elements here, you can easily see that each page corresponds to
a line here.
I've just slice it the original slice into four different slices.
All right.
Let's get back to the real.
Let me remove these court first.
OK, for the pizza guys, I'm going to declare a constant.
Because I'm going to use it multiple times.
OK?
I'm going to use a loop to print all the pages, I'll use the front variable as a starting index for
each page.
To print the elements page by page, I need to increment the from variable by the page size like this.
Now we know where to start slicing the elements.
Let's also find out the starting position to do that.
I'm going to declare a variable for this top position, then I'm going to sum the from variable and
the size like this.
So this allows me to get the next four elements for each page.
OK, let me get the items for the current page.
OK, let me print it, I will print the page header afterwards.
As you can see, until the third page, everything goes fine.
However, after that, the program crashes can guess why.
Please pass the video and try to answer.
OK, let me print indexes here.
Like this.
By the way, printing the details about an error is one of the best practical ways of debugging when
you are prototyping, OK, as you can see it first prints the elements from zero to four than four to
eight than eight to 12.
And lastly, from 12 to 16.
So we need to limit this right.
It should never exceed the length of the item slice.
To do that, I'm going to check whether the two variable is larger than the length of the items like
this.
If so, I'm going to limit it by assigning the length to the two variable like this.
OK, this will fix the problem.
Let's try it.
Cool.
Let's check out the last slicing position again.
Now it slices between 12 and 13 instead of 12 and 16.
Dude, I don't need these debugger printf anymore.
Then remove it.
OK, let me also bring together for each page I'm going to call the splinter function.
For that you can think of the splinter function as the print function.
The only difference is that instead of printing directly to the command line, it returns a formatted
string value.
Instead, it works just like print F, so you already know how it works.
Now I'm going to type the formatting string just like a regular printer formatting string, then I'm
going to pass the page.
No, of course I need to find out the current page number first.
To do that I can simply divide the from variable by the page size like this.
OK, let me pass it to the show function.
Good, but not perfect.
The first page number starts at zero and increments from there, I can quickly solve this by adding
one to this expression like this.
Cool, now it works.
All right, that's all for now.
Thank you for watching.
So far, seeing the next picture of a.