Calculating number of pages when number per page is not equal for all pages

759 Views Asked by At

Usually I'll lurk and post around at Stack Overflow, since I'm a programmer mostly, but I'm currently pulling my hair out over the lack of my mathematical knowledge.

I'm building a pagination, which can have an unequal amount of objects per page, and I need to calculate the maximum number of pages.

Let me explain in a bit more detail with an example:
Usually, I have 9 objects per page, but this can vary.
When a user does a search, he gets all his search-results first (true_results), and appended to that list, all other objects from the database (all_other_results), not included in the first set, so the page never looks empty, even with strange filters by the user.
On the page design itself, it breaks every third object into a new row, so there are 3 objects per row. After the true_results there is a horizontal line, splitting up the two sets. Since it would look ugly, if I have, for example, 7 true_results, it's two full rows and one object in the third row.

When I would only show 9 elements, it shows another row with just another two objects from the all_other_results-set, so I'm showing 10 instead on that page, to make the last row of all_other_results complete. Every following page will have 9 objects (just the last one can have less, but that's fine with rounding up).

What I know:

  • amount of true_results
  • amount of all_other_results
  • default amount of objects_per_page
  • objects per row

How do I find out the total number of pages?

This is what I have so far, which is basically me giving up and trying to brute-force it, without any luck. In some cases, I'm off by 1 (in either direction..). It looks like that:

total_1 = floor(true_results / objects_per_page)
total_2 = floor(all_other_results / objects_per_page)
if ((total_1 + total_2) * objects_per_page) < (true_results + all_other_results):
    return total_1 + total_2 + 1
else:
    return total_1 + total_2

If anyone can help me out or give a push in the right direction, it would be much appreciated!

Edit: Since I wasn't very clear, I'll show some examples:

  • When the total of both lists is divisible by 3 (=rows_per_page), all pages show 9 objects per page, and the calculation is straight forward
  • When the true_results isn't divisible by 3, the complication starts, since one of the true_results rows can either have just one or two objects, instead of three.
    • When true_results is 7: first page shows 10 results
    • When true_results is 8: first page shows 11 results
    • When true_results is 9 (or 6): first page shows 9 results
    • When true_results is 11: first page shows 9 results, second page shows 11 results (two objects of true_results and 9 of all_other_results)
1

There are 1 best solutions below

4
On BEST ANSWER

Not sure what language are you using but assuming it has a ceiling function.

total_1 = floor(true_results / objects_per_page)
remainder = true_results % objects_per_page
other_remaining_objects=0
if (remainder>0):
   remaining_rows=ceil(remainder/objects_per_row)
   other_remaining_objects=objects_per_row * (objects_per_page/objects_per_row-remaining_rows+1)
total_2 = floor((all_other_results - other_remaining_objects) / objects_per_page)
remainder = (all_other_results - other_remaining_objects) % objects_per_page
if (reminder > 0):
    return total_1 + total_2 + 2
else:
    return total_1 + total_2 + 1