Jump to content

Archived

This topic is now archived and is closed to further replies.

Wintergreen

Displaying paging links

Recommended Posts

I've got a paging system that works fine, but I'm trying to find as simple a way as possible to display the links.  I've looked through some tutorials but they all had all the links printed out, which isn't what I want. 

I'd like the links to show up like they do in this forum.  On a smaller scale, though.  So if you're on page one, it shows a few links for pages after then, then ... $last_page    If you're on page 25, it shows 1 ... 23 24 25 26 27 ... $last_page.

I've been trying for awhile but I just have a bunch of nested if statements and it seems like there really has to be a better way. 
[code]            if($page_number == 1)
              echo "1";
            else
              echo $first;
            if($max_page == 3) {
              if( ($page_number != 1) && ($page_number != $max_page) ) {
                  echo " " . $page_number;
              } else {
                  echo " " . $nav;
              }
            }
            if($max_page == 4) {
              if($page_number == 1) {
                  echo " " . $next;
              }
              if($page_number == 2) {
                  echo " " . $nav . " " . $next;
              }
              if($page_number == 3) {
                  echo " " . $prev . " " . $nav;
              }         
            }
            if($max_page > 4) {
              if($page_number == 1) {
                  echo " " . $next;
              }
              if($page_number == 2) {
                  echo " " . $nav . " " . $next;
              }
              if($page_number == 3) {
                  echo " " . $prev . " " . $nav;
              }         
            }
            if($max_page > 1) {
              if ($page_number == $max_page) {
                  echo " " . $page_number;
              } else {
                  echo " " . $last;
              }
            }
        }
[/code]

Any hints would be wonderful. 

Share this post


Link to post
Share on other sites
there's literally loads of ways of doing it. here's one i used a while back. it's definitely not perfect, but it works:

[code]
function do_crumbs($baseurl, $page, $total, $totalperpage, $crumbwidth = 5)
{
  // first calc the total number of pages
  $totalpages = ceil($total/$totalperpage);

  // crumbwidth is the amout of numbers to show on screen
  $halfwidth = floor($crumbwidth/2);

  $startpage = max(1, $page-$halfwidth);
  $endpage = min($startpage + $crumbwidth - 1, $totalpages);

  if ($totalpages > 1)
  {
      $prevlink = "Prev";
      $nextlink = "Next";

      // adding to a plain URL, or a URL with parameters already?
      $appender = stristr($baseurl, '?') ? '&':'?';

      if ($page > 1)
      {
        $prevlink = "<a href='$baseurl{$appender}p=".($page-1)."'>$prevlink</a>";
      }
      if ($page < $totalpages)
      {
        $nextlink = "<a href='$baseurl{$appender}p=".($page+1)."'>$nextlink</a>";
      }

      echo "<a href='$baseurl{$appender}p=1' style='color:#000' class='invlink'>|&lt;</a> $prevlink ";

      for ($cnt=$startpage; $cnt<=$endpage; $cnt++)
      {
        if ($page<>$cnt)
        {
            $thislink = $baseurl;

            if (stristr($baseurl, '?'))
              $thislink.='&';
            else
              $thislink.='?';
            $thislink.='p='.$cnt;
            echo "<a style='color:#000' class='invlink' href='$thislink'>$cnt</a> ";
        }
        else
            echo "[<strong>$cnt</strong>] ";
      }
      echo " $nextlink <a href='$baseurl{$appender}p=$totalpages' style='color:#000' class='invlink'>&gt;|</a>";
  }
}
[/code]

just pass it:
- the base URL (ie, the URL, with all its $_GET parameters except the 'current page' number
- the current page number
- the number of items per page
- the 'crumb width' - that is, the number of pages you want to show on the screen. optional parameter that defaults to 5.

the CSS class 'invlink' is my inverse link - ie, "do not put an underline on this link UNTIL i hover over it".

play around with it. should work in its current state, although i'd prob recommend doing some work on it.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.