"Build a Weather App" was retired on May 31, 2020.

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: Model and View Limiting the Products

Leigh Maher
Leigh Maher
21,830 Points

Why do we need less than (<) and more than (>) here?

In this if statement Randy uses greater than for when comparing the start position and the less than when comparing the end position, to the position variable.

Doesn't it have to only equal those numbers e.g. it has to be 1 - 8 to fill the first page, and 9 - 16 to fill the second page. If we use the greater than and less than signs, we're saying that it doesn't have to start at 1 for example, it just can be anything greater than it, up to the number 8.

So, what happens if somehow the values that get passed into the arguments were $positionStart (3) and $positionEnd (8). This would miss out on the first 2 products?

The code he has:

             if ($position >= $positionStart && $position <= $positionEnd) {
                $subset[] = $product;
            }

Would this not make more sense:

             if ($position = $positionStart && $position = $positionEnd) {
                $subset[] = $product;
            }

7 Answers

Let me try to explain it this way. The start point and end point of the page are not usually the same value. If they are the same, you display only one image. Since there is a spread, you cannot use equals with the start and end.

The video uses the example of wanting to return shirts 9 through 16. You have a counter that counts the shirts and the code executes its function when the value of the counter is greater than or equal to 9 and less than or equal to 16. If you say equal to 9 and equal to 16, then you get no result. The counter will never be both 9 and 16 since it can only be one value at a time. If you say equal to 9 or equal to 16, then you return shirt 9 and shirt 16, but nothing in between. Thus, you have to use greater than and less than values of some sort.

If you join the two equations with or, then you get results if the value is greater than or equal to 9 OR less than or equal to 16. That is all of the shirts because 8 is less than 16, so it is true. and 17 is greater than 9, so that is true. Thus, you have to be both greater than or equal to 9 AND less than or equal to 16.

The program returns either true or false for each evaluator. When true it returned, the loop executes. Joining them with AND means they both have to be true. Joining with OR means that either one must be true.

It is critical that you understand this logic in order to be able to properly write loops like this.

Leigh Maher
Leigh Maher
21,830 Points

Brilliant Ted. Thanks a lot. It's so obvious to me now. Great explanation. I was definitely having a mental block. For some reason I wasn't really focusing on the fact that we were looping through the products. I was just thinking what the first and last product needed to be. Thanks again.

You are welcome.

In your code, what happens if there are only 10 shirts? The display would allow the first 8, but the second page would not display because it would equal 10, not 16 (potentially as I don't remember the exact way $positionEnd is derived). Equal is much more fragile than greater than or less than.

Leigh Maher
Leigh Maher
21,830 Points

Thanks for the reply, Ted. Yes, that does make sense for positionEnd, but I'm still wondering why it's needed for the positionStart. Won't the start position always be 1, 9 etc..? If it was greater than one of these numbers it wouldn't make sense as we've set the number of products per page to exactly 8.

Again, I do not remember exactly what it looks at for the numbers, but this type of data is usually stored in a database. There would be a unique key for each product. If you use a unique key in your code, it might not start at 1 as products are removed.

Leigh Maher
Leigh Maher
21,830 Points

Hi Ted, I'm not sure it's looking at an id (or sku). I thought it was just taking the first 8 product in the array (the product data is stored in an array) as they are listed in the array.

Here's the code from shirts.php:

$start = (($current_page * $products_per_page) - $products_per_page) + 1;
$end = $current_page * $products_per_page;

$products = get_products_subset($start, $end);

The $start and $end variables are looking at the $current_page variable which from some previous lines of code produces a number, and then preforms the above calculations to decide what the start and end products should be for that page e.g. (1, 8), (9, 16). Those numbers are then passed into the this function as arguments:

function get_products_subset($positionStart, $positionEnd) {
    $subset = array();
    $all = get_products_all();
    $position = 0;

       foreach($all as $product) {
            $position ++;
             if ($position >= $positionStart && $position <= $positionEnd) {
                $subset[] = $product;
            }
        }
    return $subset;
}

As you can see the function goes through the product array and increments the position variable as it goes through each product, and when that position variable reaches the numbers that were passed into $positionStart and $positionEnd arguments, it puts those products into a subset array and returns it to the shirts.php page.

I don't see anything in this code that would suggest it looks at sku's or some kind of id?

You are making me look back at the code to see how it really works. lol

I have looked at the code and started several answers, but the real answer is obvious once I really looked at the code we are talking about.

Is it possible for $position to equal BOTH $positionStart and $positionEnd AT THE SAME TIME? No. It is impossible. Since $positionStart and $positionEnd are not equal you have to use < and > for the code to work. The $position can equal one or the other, or be in between.

I forget the name of the mathematical theorem, but if a = b and b = c, then a = c. If $position = $positionStart and $position = $positionEnd, then $positionStart = $positionEnd. That cannot work for what we are trying to do.

Leigh Maher
Leigh Maher
21,830 Points

Thanks Ted. I'm not sure I fully understand the mathematics there, but thanks for the explanation.