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

Mandy Feeney
Mandy Feeney
17,499 Points

PHP Pagination error

I am having trouble with getting my pagination code to work with redirecting when an invalid number gets put into the URL. I have my own portfolio site that shows projects I have done, so I have pagination added, which works but when I type in an invalid number into the URL, I get an error:

Warning: Cannot modify header information - headers already sent by (output started at /customers/9/9/8/monkey-in-designs.co.uk/httpd.www/inc/header.php:12) in /customers/9/9/8/monkey-in-designs.co.uk/httpd.www/projects/projects.php on line 32 The projects can not be displayed right now.

The pagination code I use to redirect follows treehouse's videos on pagination:

// Gets the page variable and sets the current page to it; Sets it to 1 if it is blank.
if(empty($_GET["page"])) {
    $current_page = 1;
} else {
    $current_page = $_GET["page"];
}

// Checks to make sure the current page is an integer and not a string; if a string then change to integer. 
$current_page = intval($current_page);

$project = new Project;

$no_of_projects = $project->count_projects();

$projects_per_page = 6;
$total_pages = ceil($no_of_projects / $projects_per_page);

// Re-directs if the page number is too high to the last page and if it's too low to the first page.
if($current_page > $total_pages){
    header("Location: ./?page=" . $total_pages);
} else if($current_page < 1){
    header("Location: ./");
}

1 Answer

Andrew Shook
Andrew Shook
31,709 Points

Mandy, I know you posted this question a few days ago, but I figured better late than never with some help. The problem here looks like it has to do with some with space left at the end of a file. Specifically, at line 12 of your header.php file. With PHP, if you have a closing PHP tag, ?>, at the end of a file, anything after that is considered output. This includes a "newline". Here is an example of what I mean.

<?php
    //your php code
?>
                     //this blank space or newline at the end of the file will be interpreted as output and php will send a response header. This will make redirecting impossible.

vs this code with no whitespace after the closing php tag, which will prevent the problem you are having.

<?php
    //your php code
?>

Technically, is doesn't have to be a newline it could also be a tab or a space. The best way to prevent this is to omit the closing PHP tag at the end of files that will only be include in other files like you header.php, or a config file.