# Calculating number of items displayed - pagination

This is my first post here and found it through Stack Overflow. I am a web designer but I am currently pulling my hair out over the lack of my mathematical knowledge.

On a site displaying a category, there may be a scenario where a user will put subcategories and products within a parent category.

On these pages (categories) where products are displayed, there is a "showing $N$ to $I$ of $T$ products" message (e.g. "showing 1 to 6 of 10 products").

So I am in a rut about how I am to calculate this to only take into account the products and not the categories.

Here's what I know (variables):

• $E =$ the current page number (there can be many pages to split up the view)

• $F =$ the amount of products or subcategories allowed on any 1 page

• $Y =$ the total amount of subcategories being displayed in this category

• $L =$ the total amount of products displayed in this category

Also the subcategories are always displayed first before the products.

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

EDIT

as per the solution below, here is the PHP interpritation: (valiables are in relation to the post and relating comments)

````function f( $x, $f, $y, $l ) {
    return ( ( $x * $f ) - $y < 0 ? 0 : ( ( $x * $f ) - $y > $l ? $l : ( $x * $f ) - $y ) );
}

$n = 1 + f($e-1, $f, $y, $l);
$i = f($e, $f, $y, $l);
echo 'Showing ' . $n .' to ' . $i . ' of ' . $l . ' Products';
````
-

## 3 Answers

If I understand you correctly, a category can contain two kinds of items: subcategories and products. Out of those, subcategories are always sorted before products. On each page, you display (up to) $F$ items, and, for the $E$-th page, you want to know

• $N =$ the total number of products displayed on pages $1$ to $E-1$ plus one, and
• $I =$ the total number of products displayed on pages $1$ to $E$.

Clearly, there are $E$ times $F$ items (products or subcategories) displayed on the pages $1$ to $E$ (unless this would exceed $L+Y$, in which case that's the upper limit). Out of those, up to $Y$ will be subcategories, so we're left with $EF-Y$ products (unless $EF<Y$, in which case there will be no products on those pages).

So, let's use $f(X)$ to denote the total number of products displayed on pages $1$ to $X$. Then

$$f(X) = \begin{cases} 0 & \text{if } XF-Y < 0 \\ L & \text{if } XF-Y > L \\ XF-Y & \text{otherwise}. \end{cases}$$

Using this rule, we can then easily calculate $N = 1 + f(E-1)$ and $I = f(E)$.

(Ps. Note that, if $Y \ge F$, using this rule the first page would carry a message saying "showing $1$ to $0$ of $L$ products". You may want to have a separate message for the case $I=0$ if there's any chance that it could occur in practice.)

-

this is the most detailed message I have ever recieved on ANY forum... I'm just going to spend a few minutes finding out what the squiggly *f*() means... brb – Phil Jackson Sep 28 '11 at 20:08
think it means function and X being the passing value of total products... hope so – Phil Jackson Sep 28 '11 at 20:11
Yes, the "squiggly $f$" is just the name of a function.  Its argument $X$ is the number of a page; I used $X$ there instead of $E$ to avoid confusion below, where we pass $E-1$ as the argument to $f$ when calculating $N$. – Ilmari Karonen Sep 28 '11 at 20:11
one more thing (trying to remember what a teacher tried to teach me many moons ago..) does XF mean X*F... sorry if im being a pain – Phil Jackson Sep 28 '11 at 20:14
– Ilmari Karonen Sep 28 '11 at 20:26
show 1 more comment

When you don't have categories, the number of pages is $\frac{L}{F}$, rounded up to the next whole number. So if there are 53 products and you display 8 per page, you will need 7 pages. On page E, you will have objects (E-1)*F+1 to E*F.If you have categories, does each one start on a new page? Then you just have the same calculation for each category. If your 53 objects are in categories of 12,23,5, and 13, you would have 2,3,1, and 2 pages, for a total of 8. You then have to decide whether the showing N to P of M is within the category or over all products. Is this what you were after?

-

if there are lets say 3 sub cats and 6 prods and each page only allows 6 items per page to be displayed, then 3 cats and 3 prods would be on the first page (showing 1 to 3 of 6 products) and 3 on the next. – Phil Jackson Sep 28 '11 at 19:30
I think I may have sused N = ( ( ( E * F ) - ( F - 1 ) ) - Y ); – Phil Jackson Sep 28 '11 at 19:31

Also the subcategories are always displayed first before the products.

The alarm bell wrang after reading that line, it is not a requirement of the problem but just a viewing of the results. More than math looking at Design Patterns will help you.

The mathematical solution of this problem is completely wrong for Web Programming.

First separate your entities i.e. divid your problem to Categories, Products etc., 2nd solve the problem for each group separately, Last display the products and categories in a way that they are not coupled ( i.e. choosing one does not effect what you see in other one).

Lastly try asp.net, there were pre made samples that have already this scenario implemented. This is not a job for a web designer but a programmer. If this is a job for a client you will be way behind by the time it is gonna take to design, implement and test this approach, get a php shopping card close to what you need, then ponder about the math part separately.

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 41, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.8948853611946106, "perplexity_flag": "middle"}