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 Invalid Page Numbers

Clean URL when ?pg is specifically set to 0

When the page is loaded as shirts/?pg=0, I want it to redirect to just shirts/, but when I try that, I get an infinite redirect. How could I do this?

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

Yes, because you are redirecting the page to itself, which is in turn redirecting the page to itself. If you want to remove the query string from the url for the first page simply don't add the query string to the url for the page. You'll need to add a bit of php that checks to see if $_GET['pg'] isset and equal to zero to make that happen. If your shirts.php code up here I can show you how to modify it.

<?php

    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);
    }

    if ($current_page < 1) {
        header("Location: ./");
    }

    $products = get_products_all();

?><?php 
$pageTitle = "Mike's Full Catalog of 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') ?>

Hint 1: Instead of making a rule for every invalid page number to redirect (infinite), it's better to redirect only the page numbers that are not in the collection.

Hint 2: When you don't set the pg variable, what do you think the value of the pg variable is? Try to output it. You'll understand why it keeps redirecting.

I know that it's redirecting because the default pg variable is 0.

But I don't understand what you mean for hint 1

Hint 1 was before I saw your code so you can ignore it. Like Andrew said, add a check to see if $_GET["pg"] is set and equal to zero, if that's the case, redirect to shirts.

<?php

if (isset($_GET["pg"]) && $_GET["pg"] == 0) {
    header("Location: ./");
}

Ah now I understand! Thank you, works perfectly