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 Integrating PHP with Databases Limiting Records in SQL Pagination Function

Alfredo Prince
Alfredo Prince
6,175 Points

What does it mean by "return the pagination value"

Am I supposed to use a literal return? Where?

index.php
<?php
include "pagination.php";

/* add function here */
function pagination ($total_pages, $current_page, $section, $search){
$pagination = "<div class=\"pagination\">";
$pagination .= "Pages: ";  
for ($i = 1;$i <= $total_pages;$i++) {
    if ($i == $current_page) {
        $pagination .= " <span>$i</span>";
    } else {
        $pagination .= " <a href='catalog.php?";
        if (!empty($search)) {
            $pagination .= "s=".urlencode(htmlspecialchars($search)) . '&' ;
        } else if (!empty($section)) {
            $pagination .= "cat=".$section . '&';
        }
        $pagination .= "pg=$i'>$i</a>";
    }
}
$pagination .= "</div>";}
/* setting pagination variable */


/* displaying the pagination */
echo $pagination;

1 Answer

Simon Woodard
Simon Woodard
6,545 Points

You've created the function correctly but remember it wont do anything by itself, you also need to execute (call) the function for it to run.

It would seem that in this particular challenge you actually need to 'return $pagination' instead of 'echo $pagination' inside the function. Then after closing the function you can execute it with the following command and set of arguments (or required variables).

echo pagination(Total Pages, Current Page, Section, Search);

Where you have replaced the arguments with the correct values that the course requires.

Alfredo Prince
Alfredo Prince
6,175 Points

this is what i did and it says "Task 1 no longer passing"

include "pagination.php";

/* add function here */
function pagination($total_pages, $current_page,$section,$search){
  $pagination = "<div class=\"pagination\">";
$pagination .= "Pages: ";  
for ($i = 1;$i <= $total_pages;$i++) {
    if ($i == $current_page) {
        $pagination .= " <span>$i</span>";
    } else {
        $pagination .= " <a href='catalog.php?";
        if (!empty($search)) {
            $pagination .= "s=".urlencode(htmlspecialchars($search)) . '&' ;
        } else if (!empty($section)) {
            $pagination .= "cat=".$section . '&';
        }
        $pagination .= "pg=$i'>$i</a>";
    }
}
$pagination .= "</div>";
}
/* setting pagination variable */
$pagination = pagination();

/* displaying the pagination */
return pagination($total_pages, $current_page,$section,$search); ```
Alfredo Prince
Alfredo Prince
6,175 Points

I tried what you said and it says "task 1 is no longer passing"

include "pagination.php";

/* add function here */
function pagination($total_pages, $current_page,$section,$search){
  $pagination = "<div class=\"pagination\">";
$pagination .= "Pages: ";  
for ($i = 1;$i <= $total_pages;$i++) {
    if ($i == $current_page) {
        $pagination .= " <span>$i</span>";
    } else {
        $pagination .= " <a href='catalog.php?";
        if (!empty($search)) {
            $pagination .= "s=".urlencode(htmlspecialchars($search)) . '&' ;
        } else if (!empty($section)) {
            $pagination .= "cat=".$section . '&';
        }
        $pagination .= "pg=$i'>$i</a>";
    }
}
$pagination .= "</div>";
}
/* setting pagination variable */
$pagination = pagination();

/* displaying the pagination */
return pagination($total_pages, $current_page,$section,$search);