Jump to content


Photo

Displaying paging links


  • Please log in to reply
1 reply to this topic

#1 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 12 September 2006 - 06:02 PM

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. 
            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;
               }
            }
         }

Any hints would be wonderful. 

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 12 September 2006 - 07:52 PM

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:

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>";
   }
}

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.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users