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 Setting LIMITs

György Varga
György Varga
19,198 Points

Too many redirects

Hi! Can you please help me identify the issue?

https://w.trhou.se/40it74wdff

Thanks!

Steven Jackson
Steven Jackson
11,465 Points

I know this is pretty late, but my issue came from my re-directs. In the first example when a category is selected both re-directs are attempting to run. This throws the TOO_MANY_REDIRECTS error. In the second example, the code has been corrected running only one re-direct when a category is selected.

Keep in mind that if both of the redirects are running at the same time for any reason we will see this error.

Hope this helps!

<?php

/* Original Code */ 
//Rediret too-large page numbers to the last page
if($current_pagec> $total_pages){
    header("location:catalog.php?"
    //Category limit results
    . $limit_results 
    ."pg=" . $total_pages);
}

//Redirect too-small page numbers to the first page
if($current_page > $total_pages){
    header("location:catalog.php?" 
        //Category limit results
        . $limit_results 
        . "pg=1" . $total_pages
    );
}

/* Corrected Code */
//Rediret too-large page numbers to the last page
if($current_page > $total_pages){
    header("location:catalog.php?"
    //Category limit results
    . $limit_results 
    ."pg=" . $total_pages);
}

//Redirect too-small page numbers to the first page
if($current_page < 1){
    header("location:catalog.php?" 
        //Category limit results
        . $limit_results 
        . "pg=1" . $total_pages
    );
}

?>