0

I am not sure how this works, hence need a little help. I am fetching some nodes using db_select and rendering this in a foreach loop in a custom template file. I would give a hint of my code :

$nodes = db_select('node', 'n')
// Some fields...
// Some conditions...
// ->execute();
$nodes = $nodes->fetchAll();
return theme('my_custom_callback', array('nodes' => $nodes));

Now I get this $nodes in my tpl file and print them to the design. But what I want is to have the Drupal default pagination effect (probably using the PagerDefault facility). I have come accross many examples of the use of PagerDefault but I am not sure how to render the pagination in my tpl file.

Please note using a Drupal view or a table theme is not an option in my case.

Help appreciated...

0

Well as no one responded, I searched a bit and found a normal PHP function to achieve the same. I know it may not be a Drupal way, but its for them who are looking for a quick solution for now (like me). But would like to add that other answers to achieve this the specially the Drupal way is very much welcome. Also please note, I almost copy pasted the actual function, so you probably may want to modify that as per your UI (for actual function I have given the reference below) :

// This is the menu callback where $data are the records fetched to be displayed in this page, and $total_count is the total number of count of all the records.

$pager = pagination($current_page, $total_count, $record_limit, 2, "url-menu-page?page=%d");

$return_val = theme('my_callback_function', array('data' => $data, 'pager' => $pager));

return $return_val;

And here goes the actual pagination building function :

/**
 * Displays pagination links based on given parameters
 *
 * @param int $currentPage - current page
 * @param int $itemCount - number of items to paginate, used to calculate total number of pages
 * @param int $itemsPerPage - number of items per page, used to calculate total number of pages
 * @param int $adjacentCount - half the number of page links displayed adjacent to the current page
 * @param (string|callable) $pageLinkTemplate - pagination URL string containing %d placeholder or a callable function that accepts page number and returns page URL
 * @param boolean $showPrevNext - whether to show previous and next page links
 * @return void
 */
function pagination($currentPage, $itemCount, $itemsPerPage, $adjacentCount, $pageLinkTemplate, $showPrevNext = true) {
  $pager = '';
  $firstPage = 1;
  $lastPage  = ceil($itemCount / $itemsPerPage);
  if ($lastPage == 1) {
      return;
  }
  if ($currentPage <= $adjacentCount + $adjacentCount) {
      $firstAdjacentPage = $firstPage;
      $lastAdjacentPage  = min($firstPage + $adjacentCount + $adjacentCount, $lastPage);
  } elseif ($currentPage > $lastPage - $adjacentCount - $adjacentCount) {
      $lastAdjacentPage  = $lastPage;
      $firstAdjacentPage = $lastPage - $adjacentCount - $adjacentCount;
  } else {
      $firstAdjacentPage = $currentPage - $adjacentCount;
      $lastAdjacentPage  = $currentPage + $adjacentCount;
  }
  $pager = '<div class="pagination-hld"><h2 class="element-invisible">Pages</h2><div class="item-list"><ul class="pager">';
  if ($currentPage != $firstPage) {
    $pager .=  '<li class="pager-first first"><a href="' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($firstPage) : sprintf($pageLinkTemplate, $firstPage)) . '">&laquo; first</a></li>';  
  }

  if ($showPrevNext) {
      if ($currentPage == $firstPage) {
      //$pager .= '<li title="Go to first page" class="pager-first first">< previous</li>';
      } else {
      $pager .= '<li title="Go to previous page" class="pager-previous"><a title="Go to previous page" href="' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($currentPage - 1) : sprintf($pageLinkTemplate, $currentPage - 1)) . '">&lsaquo; previous</a></li>';
      }
  }
  if ($firstAdjacentPage > $firstPage) {
      if ($firstAdjacentPage > $firstPage + 1) {
      $pager .=  '<span>...</span>';
      }
  }
  for ($i = $firstAdjacentPage; $i <= $lastAdjacentPage; $i++) {
      if ($currentPage == $i) {
      $pager .=  '<li class="pager-current">' . $i . '</li>';
      } else {
      $pager .=  '<li class="pager-item"><a title="Go to page '.$i.'" href="' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($i) : sprintf($pageLinkTemplate, $i)) . '">' . $i . '</a></li>';
      //$pager .=  "<li class='pager-item'><a title=\"Go to page $i\" href=\"' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($i) : sprintf($pageLinkTemplate, $i)) . '\">$i</a></li>\r\n";
      }
  }
  if ($lastAdjacentPage < $lastPage) {
      if ($lastAdjacentPage < $lastPage - 1) {
      $pager .=  '<span>...</span>';
      }
  }
  if ($showPrevNext) {
      if ($currentPage == $lastPage) {
      //$pager .=  '<li class="pager-next"><span>next ></span></li>';
      } else {
      $pager .=  '<li class="pager-next"><a href="' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($currentPage + 1) : sprintf($pageLinkTemplate, $currentPage + 1)) . '">next &rsaquo;</a></li>';
      }
  }
  if ($currentPage != $lastPage) {
    $pager .=  '<li class="pager-last last"><a href="' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($lastPage) : sprintf($pageLinkTemplate, $lastPage)) . '">last &raquo;</a></li>';
  }
  $pager .=  '</ul></div></div>';

  return $pager;
}

Hope this helps somebody...

Original code credit goes to the author of this blog : http://salman-w.blogspot.in/2014/04/stackoverflow-like-pagination.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.