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 Integrating PHP with Databases Limiting Records in SQL Calculations and Links

Dan Avramescu
Dan Avramescu
11,286 Points

I think that here might appear a bug

if (isset($_GET['pg'])) {
    $current_page = filter_input(INPUT_GET,'pg',FILTER_SANITIZE_NUMBER_INT);
}
if (empty($_GET['pg'])) {
    $current_page = 1;
}

this code should have elseif :

if (isset($_GET['pg'])) {
    $current_page = filter_input(INPUT_GET,'pg',FILTER_SANITIZE_NUMBER_INT);
} elseif (empty($_GET['pg'])) {
    $current_page = 1;
}

because if request is GET ?cat='books'&pg=0, the (empty($_GET['pg']) is true so $current_page gets reassigned to 1, so the part where we redirect to the first page when the page requested is smaller than the first page won't work because the first line from

if($current_page < 1){
    header("location:catalog.php?"
        . $limitTo
        . "pg=1");
}

evaluates to false, then no redirect happens. I am asking this in order to get feedback if I am wrong. Thanks!

Alex Bauer
Alex Bauer
9,426 Points

I know this is a late response but I noticed that you put $limitTo instead of $limit_results, $limitTo is an undefined variable.

1 Answer

That's true, I was actually testing the pg variable in the URL and when using 0 as a value it didn't redirect I looked at the code and couldn't find out the error and this explains everything. Thanks for posting this