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


Chris Dziewa
Chris Dziewa
17,781 Points

Paginating a List: Controller > Adjusting Larger Page Numbers

Hey guys I am having trouble getting my header function to redirect to the last page (pg 4) of my shirts section when someone changes the url page to something too large. I have checked my products include file for any errors and it is the same as the downloaded project files. Every time I try to input a value that should redirect, it doesn't. I had a similar issue with .htaccess redirects on a couple of the shirt detail pages which after a while, and without changing any code, it worked fine. I have included the top portion of my shirts.php file which seems to be the cause of my problem. Here is the Adjusting Larger Page Numbers video. I would prefer not to do a codepen if I don't have to. If the code below is correct, could the problem be with my MAMP local server? Thanks guys!

require_once(ROOT_PATH . "inc/products.php");

if (empty($_GET["pg"])) {
    $current_page = 1;
} else {
    $current_page = $_GET["pg"];

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

if ($current_page > $total_pages) {
    header("Location: ./?pg=" . $total_pages);

echo "<pre>";
echo "Total Products: ";
echo $total_products;
echo "\nTotal Pages:";
echo $total_pages;
echo "\nCurrent Page: "; 
echo $current_page;

1 Answer

Chris Dziewa
Chris Dziewa
17,781 Points

Nevermind, it is working now. I think this was just a local server issue. It didn't like that file or something. I moved it around and saved it again. After a couple hours now it is working how it should. I even compared the two documents with DiffNow (which is an awesome site by the way-definitely bookmarking it) and wasn't missing anything that would inhibit functionality. Very strange.