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

Can someone please tell me what's wrong with my code here?

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

/* add function here */ function pagination($total_pages, $current_pages,$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 */ retrurn $pagination; }

echo pagination($total_pages, $current_pages,$section,$search); ``1

Please and thank you.

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

/* add function here */
function pagination($total_pages, $current_pages,$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 */
retrurn  $pagination;
}

echo pagination($total_pages, $current_pages,$section,$search);

right away I see a typo retrurn $pagination; should be return $pagination;

2 Answers

Shari,

This one was a pain. The problem with these code challenges is, you could get it right but not exactly as they want it.

Here is the way they wanted it done.

<?php
include "pagination.php";

/* add function here */
function pagination($total_pages, $current_page, $section, $search) {

  /* setting pagination variable */
  $pagination = "<div class=\"pagination\">";
  $pagination .= "Pages: ";  
  $i=0;
  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 */
echo $pagination;
}
pagination($total_pages, $current_page, $section, $search);

Thank you! It worked!