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

Artan Muzhaqi
Artan Muzhaqi
7,983 Points

task 1 is not working when i go to task 2

my code is not working

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

/* add function here */
function pagination($total_pages,$current_page,$section,$search){
  /* setting pagination variable */
$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>";

/* displaying the pagination */
return $pagination;
}

echo pagination(10,4,"artan","test");

2 Answers

Ezra Siton
Ezra Siton
12,644 Points

In the 1/2 you create the function. Look at the mission:

To use a function for displaying the pagination, we need to pass our function the required variables."

10....4....artan and test are not $vars Change this:

/* displaying the pagination */
echo pagination(10,4,"artan","test");

To this (this is scope trick . Now you pass to the function req vars):

/* displaying the pagination */
echo pagination($total_pages, $current_page, $section, $search);
Artan Muzhaqi
Artan Muzhaqi
7,983 Points

thank you so much. i didn't get that part i just thought that i could pass any variables just to test it.

Ezra Siton
Ezra Siton
12,644 Points

One more useful example:

<?php
function foo($var)
{
    $var++;
    echo $var;
    // local scope - result "6";
}

$var=5; 
foo($var);
// $var still 5 !!!
echo $var;
// result: 5
?>

Test this idea her: http://www.writephponline.com/

If you want to change the global $var use this: http://php.net/manual/en/language.references.pass.php