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 trialJohn Shockey
45,061 PointsPagination Function
This is such an easy objective.... except I can't seem to figure out the last step?
- Move all the code that sets the $pagination variable inside the pagination function.
- Return the $pagination variable.
- Change the echo on the final line to call the function instead of the variable. Make sure you pass the four variables: \$total_pages, \$current_page, \$section, \$search.
I'm getting this error: Bummer! PHP Parse error: syntax error, unexpected '$total_pages' (T_VARIABLE), expecting identifier (T_STRING) in /workdir/index.php on line 28 Parse error: syntax error, unexpected '$total_pages' (T_VARIABLE), expecting identifier (T_STRING) in /workdir/index.php on line 28
<?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>";
return $pagination;
}
/* displaying the pagination */
pagination(\$total_pages, \$current_page, \$section, \$search);
2 Answers
Anthony Babich
5,505 PointsI fooled around with it a little and completed the challenge :D looks like you didn't need to break out with \ on line 28
<?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>";
return $pagination;
}
/* displaying the pagination */
echo pagination($total_pages, $current_page, $section, $search);
Ashish Bharti
5,353 Pointsthanks
John Shockey
45,061 PointsJohn Shockey
45,061 PointsI swear before I made this post I had written the exact answer you provided and it kept giving me: Task 1 is no longer passing! But it worked this go around.... thanks!