The query API allows us to retrieve data based on pagination as well. To demonstrate the same, let's go and comment this and write a new function called Get Products by page. Let's supply the page number as three, assuming that each page has 10 products, so it is like page number equals to three, and page size happens to be a 10. If you want to supply that also here, that is fine, but I just want to supply the page number here. Since the function doesn't exist, it's given me a quick fix called create method called Get Products by page. I select that, press tab.
Select the package level then why it is fine with me and then this is int is also okay. I'm going to call this as page and num and press enter. So we have got the function body ready here. First, let's add the query which is string SQL equals two from product. I don't need to do anything else over here because I want to say give me all the products but not specifying the limit and offset over here, I'm going to assume the limit to be around 10. But offset is based on the page number.
So if I want to redo the details of the page number three, I should start from 21 to 30, which means I have to skip the first 20 Records. So if this is yen, I have to say n minus one into limit. So I'm going to type here in offset equals to page num, minus one multiplied by the limit. Now I can supply this to our query. So I'm going to copy this part, paste over here. I can say query dot set first result to the offset, and then I say q r dot set max result to the limit and all I have to do is to Get this list and then loop through that and print the details.
Instead of displaying the product name and price, I also like to say P dot get product ID plus some hyphen. Plus, the reason is that IDs are automatically ordered over here, which means I will be able to know that I'm actually getting the data from the page number three, so save and run the same. I should now see the page number three details where 21 to 30 products have been displayed. If I go here and say, give me the page number five, I should now see from 41 to 50. And it's giving the result as expected.