Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Paginating a List: Controller Calculating Shirts from Page Number

Giovanni Valdenegro
Giovanni Valdenegro
13,871 Points

Formula for last page values to being 8 per page?

I am confused with the if statement: if ($end > $total_products) { $end = $total_products; }

So:

if we have 4 pages x 8 products = 32 products.

So how is it possible that:

if (32 > 32) { $end = 32 }

I dont get it how $end can possibly be greater than total products? I undertstand that total products already has the maximum number so how can end exceed that number?

3 Answers

If you took away the conditional if statement the $end variable would always be calculating the current page by total products per page which is always 8.

If you then added an item to your products array you would have 33 total products which would give you a total of 5 pages.

When you navigate to the 5th page, the $end variable would then multiply the current page (5) by the total number of products per page (8). This would effectively set $end to 40 which is greater than the total products.

So the issue is that the code without the conditional thinks you have 8 products on the 5th page to display when in reality you only have 1.

I guess it's up to you if you find that beneficial to have the conditional.

Giovanni Valdenegro
Giovanni Valdenegro
13,871 Points

Thanks Jeremy now it makes sense. I was thinking only in the current scenario and not applying it at a dynamic level.

Kang-Kyu Lee
Kang-Kyu Lee
52,045 Points

In case the value $total_products was between 25 and 31, $total_pages would be 4 (when $products_per_page equals 8) :

$total_pages = ceil($total_products / $products_per_page);

If without that if statement, $end would be 32 for the last page 4 :

$end = $current_page * $products_per_page;

and then a number in the range (25..31) would be smaller than $end -- hopefully it could help.