Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

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("../inc/config.php");
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;
exit;

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.