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: Controller Adjusting Larger Page Numbers

[SOLVED] Header Redirect Not Working

Everything else is working fine, but my PHP header call is not working. Here is my shirts.php file:

<?php

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

    $current_page = "";

    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: ";
    var_dump($total_products);
    echo "\nTotal Pages: ";
    var_dump($total_pages);
    echo "\nCurrent Page: ";
    var_dump($current_page);
    exit;




    $products = get_products_all();

?><?php
    $pageTitle = "Mike's Shirts";
    $section = "shirts";
    include(ROOT_PATH . "inc/header.php"); 
?>

    <div class="section shirts page">

        <div class="wrapper">

            <h1>Mike&rsquo;s Full Catalog of Shirts</h1>

            <ul class="products">
                <?php foreach($products as $product) { 
                        echo get_list_view_html($product);
                    } 
                ?>

            </ul>

        </div>

    </div>

<?php 
include(ROOT_PATH . "inc/footer.php"); 
?>

Everything works, except for I am not redirected to the ?pg=4 page. I even added a statement to the conditional that contains the redirect that said echo "Needs redirect". This command ran.

Then, I tried redirecting to a different URL all together: http://example.com. The redirect did not work.

Anyone have an idea of what the problem might be?

Thanks in advance!

John Sanchez

1 Answer

Nevermind, I solved it -- I left a blank line after the closing ?> on my products include file.

Mark Germani
Mark Germani
14,399 Points

Ah! I had just left a comment (which I subsequently deleted, after seeing that you'd solved it), in which I wondered if it was your products include file. I had tried your shirts.php code within my copy of the website and it worked every time. Glad you fixed it!