2

I need to get 29 products on page #1 and need to get 26 products from page #2 to the last page number without duplication.

I'm setting page size as 29 in the first page and 26 from the second page, the issue here is 3 products will be appearing again in the second page as we are setting 26 as the page size. for example, we have 56 products.

page number #1, we are getting 29 products from 1-29.
page number #2, we are getting 26 products from 27-52.

here 27 - 29 products will be coming again in 2nd page.

how to solve this?

1

3 Answers 3

2

Some of the Best ways to limit your collection size are:

setPageSize()

This function will limit the number of results that are returned – basically using a LIMIT X mysqli query. For example:

$collection->setPageSize(10);
echo $collection->count(); // will echo 10

setPage()

setPage() is similar to using setPageSize but you can add two parameters – just like you would with mysqli LIMIT 10,5 for example. The first parameter being the start from point – the next being how many you wish to bring back:

$collection->setPage(0,20); // will bring back the first 20 products
$collection->setPage(10,5); // will bring back 5 products after the 10th product

getSelect()->limit()

This function allows you to add your own limit to the select query – its basically the same as entering LIMIT X. For example:

$collection->getSelect()->limit(20); // will bring back our first 20 results

A handy hint to see what these limits are doing to your mysqli query is to echo out your mysqli query in the code. To do this you can simply perform the following after you’ve tweaked your $collection object:

echo $collection->getSelect()

For Second $limit && $offset Example

You imagen you run one query like :

$sql = "SELECT * FROM Orders LIMIT 30";

When the SQL query above is run, it will return the first 30 records

What if we want to select records 16 - 25 (inclusive)?

Mysqli also provides a way to handle this: by using OFFSET.

The SQL query below says "return only 10 records, start on record 16 (OFFSET 15)":

$sql = "SELECT * FROM Orders LIMIT 10 OFFSET 15";

In short in your collection add $limit && $offset.And above I explain how work $limit && $offset .

I Hope you are understand and solve your issue.

THANKS.

0
1

I think you can achieve this by adding custom limit to product collection:

$firstPageSize = 29;
$pageSize = 26;
$currentPage = getCurrentPage();
if ($currentPage <= 1) {
    // function limit($count = null, $offset = null);
    $productCollection->getSelect()->limit($firstPageSize);
} else {
    $productCollection->getSelect()->limit($pageSize, $firstPageSize + $pageSize * ($currentPage - 2));
}

foreach ($productCollection as $product) {
    // do the stuff
}
1
  • This solution didn't work, I have tried by adding in Listproduct.php Jul 13, 2020 at 8:29
0

You'd play with limit & offset

$collection->getSelect()->limit($limit, $offset);

$limit will be the number of records to return

$offset will be the starting point

So, for your first page request would be

$collection->getSelect()->limit(29, 0); // or just $collection->getSelect()->limit(29);

And for the second one

$collection->getSelect()->limit(26, 29);

And so on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.