Pagination help

Discussion in 'PHP' started by liam1412, Sep 17, 2008.

  1. #1
    Please can someone help with this pagination someone did for me


    The code to calculate is

    
    
    if(isset($_GET['pageno'])){
    $page_no = $_GET['pageno'];
    } else {
    $page_no = 1;
    }
    
    $rows_per_page = 10;
    $last_page = ceil($numreplies/$rows_per_page);
    
    if($page_no < 1){
    $page_no = 1;
    } elseif ($page_no > $last_page){
    $page_no = $last_page;
    }
    
    $page = ($page_no - 1) * $rows_per_page;
    
    
    PHP:
    Then the code for the display is

    
              if ($page_no <= 1) {
                 echo "&#171; First";
              } else {
                 echo "&#171; <a href='{$_SERVER['PHP_SELF']}?threadid=$postid&pageno=1'>First</a>";
              } 
    
                for($a = 1; $a <= $last_page; $a++){
                  if($a == $page_no) {
                    echo ' <b>['.$a.']</b> '; 
                  } else {
                    echo ' <a href="viewthread.php?threadid='.$postid.'&pageno='.$a.'">'.$a.'</a> ';
                  } 
                } 
    
    
              if ($page_no == $last_page) {
                 echo "Last &#187;";
              } else {
                 echo '<a href='.$_SERVER['PHP_SELF'].'?threadid='.$postid.'&pageno='.$last_page.'">Last</a> &#187;';
              }
    
    PHP:
    This will display as

    << first [1] 2 3 4 5 Last >>

    but this will go in forever.

    so if there was 20 pages it would be

    << first [1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 last >>

    What I would like is if I was on page 1 it would show

    << first [1] 2 3 4 5 ... 18 19 20 >>

    or if I was on the 10th page it would show << first 8 9 [10] 11 12 ... 18 19 20 last >>

    so it would show the 2 before the current page the current page in brackets the following 2 pages the last 2 pages.

    Is that a quick fix and can anyone help me with this.
     
    liam1412, Sep 17, 2008 IP
  2. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #2
    try this,
    it should give about what you want.
    peace

    if ($page_no <= 1) {
                 echo "« First";
              } else {
                 echo "« <a href='{$_SERVER['PHP_SELF']}?threadid=$postid&pageno=1'>First</a>";
              }
    
                for($a = 1; $a <= $last_page; $a++){
    if($a==$page_no-5) { $ppprint=1; }
    if($a==$page_no+5) { $ppprint=0; }
    if($ppprint==1) {
                  if($a == $page_no) {
                    echo ' <b>['.$a.']</b> ';
                  } else {
                    echo ' <a href="viewthread.php?threadid='.$postid.'&pageno='.$a.'">'.$a.'</a> ';
                  }
    }
                }
    
    
              if ($page_no == $last_page) {
                 echo "Last »";
              } else {
                 echo '<a href='.$_SERVER['PHP_SELF'].'?threadid='.$postid.'&pageno='.$last_page.'">Last</a> »';
              }
    Code (markup):
     
    happpy, Sep 17, 2008 IP