Jump to content

Pagination


jfarthing

Recommended Posts

I repeat, the tutorial shows you far more than enough to accomplish the same look and feel as to the link you posted. Page 4 of the tutorial shows how to arrive at the total number of pages that are available. That's the only number you need to get the rest of the magic. Let's state, in plain English, what the bar is doing on the page you provided.

 

Using the limit number, the first column should show the range of data results for the current page. If our limit is 25, page 1 should display 1 - 25, page 2 should show 26 - 50, etc. Very simple math gets you this display, using limit number as the common denominator.

 

The next column displays an image representing 'back one page' if not on page 1. Can be represented as text with <<

 

The next column displays the pagination proper.

 

If on page 1, it should display up to 4 pages ahead (if that many available - use total number of pages to check for this).

 

Clicking on pages 2 - 4 retain a display of 1 - 5, we only remove a hyperlink for the current page.

 

Clicking on page 5 changes the bar, and all pages beyond 5 maintain this new change.

  - Page 1 link always remains.

  - A new bar is built with the current page (no link) in the middle, and 2 pages forward and backward from current page.

  - Tack page one on the front of that new bar.

 

You now have the formula for the display of all pages greater than 4.

 

Tacked on to the end of every bar except the last page is an ellipsis (...) followed by the last page available (simply, total number of pages). The last 2 pages are slightly different.

 

There is a page forward graphic in the last column. Can be represented in text as >>

 

Ok... all this stuff is simply presentation. The math to get the bar presented was provided in the tutorial. It is beyond any tutorial to provide every way possible to pimp out your HTML, that's up to you. By following along the plain English above, you should be able to re-create that bar EXACTLY, and quite easily! Simply define the limit, let PHP discover the total number, and then track the current page. Kindergarten stuff! hehe

 

PhREEEk

Link to comment
Share on other sites

  • 2 weeks later...

Well, I came up with something.  Don't know if there is a cleaner, more logical approach...

 

<?php

function pages($url,$total_items,$per_page,$current_page) {

$total_pages = ceil($total_items / $per_page);
$prev_page = ($current_page - 1);
$next_page = ($current_page + 1);

if($current_page != 1) {
  $return = ' <a href="'.$url.'?page='.$prev_page.'" title="Previous Page">«</a> ';
}

$return .= 'Page: ';

if($current_page != 1) {
  $return .= ' <a href="'.$url.'"?page=1" title="First Page">1</a> |';
} else {
  $return .= ' 1 |';
}

if ($current_page < 5) {
  if ($total_pages < 5) {
    for ($i=2;$i<=$total_pages;$i++) {
      if ($i == $current_page) {
        $return .= ' '.$i.' |';
      } else {
        $return .= ' <a href="'.$url.'?page='.$i.'" title="Page '.$i.'">'.$i.'</a> |';
      }
    }
  } else {
    for ($i=2;$i<=5;$i++) {
      if ($i == $current_page) {
        $return .= ' '.$i.' |';
      } else {
        $return .= ' <a href="'.$url.'?page='.$i.'" title="Page '.$i.'">'.$i.'</a> |';
      }
    }
  }
  if ($current_page < $total_pages) {
    if ($total_pages > 5) {
      $return .= ' ... | <a href="'.$url.'?page='.$total_pages.'" title="Last Page">'.$total_pages.'</a> |';
    }
    $return .= ' <a href="'.$url.'?page='.$next_page.'" title="Next Page">»</a> ';
  }
}

if (($total_pages > 5) && ($current_page >= 5) && ($current_page < ($total_pages-2))) {
  for ($i=($current_page-2);$i<=$current_page;$i++) {
    if ($i == $current_page) {
      $return .= ' '.$i.' |';
    } else {
      $return .= ' <a href="'.$url.'?page='.$i.'" title="Page '.$i.'">'.$i.'</a> |';
    }
  }
  for ($i=$current_page+1;$i<=$current_page+2;$i++) {
    if ($current_page >= $total_pages) {
      break;
    } else {
      $return .= ' <a href="'.$url.'?page='.$i.'" title="Page '.$i.'">'.$i.'</a> |';
    }
  }
  if ($current_page < $total_pages) {
    $return .= ' ... | <a href="'.$url.'?page='.$total_pages.'" title="Last Page">'.$total_pages.'</a> | <a href="'.$url.'?page='.$next_page.'" title="Next Page">»</a> ';
  }
}

if (($total_pages > 5) && ($current_page == ($total_pages-2))) {
  for ($i=($current_page-2);$i<=$current_page;$i++) {
    if ($i == $current_page) {
      $return .= ' '.$i.' |';
    } else {
      $return .= ' <a href="'.$url.'?page='.$i.'" title="Page '.$i.'">'.$i.'</a> |';
    }
  }
  for ($i=$current_page+1;$i<=$total_pages;$i++) {
    if ($current_page >= $total_pages) {
      break;
    } else {
      $return .= ' <a href="'.$url.'?page='.$i.'" title="Page '.$i.'">'.$i.'</a> |';
    }
  }
  if ($current_page < $total_pages) {
    $return .= ' <a href="'.$url.'?page='.$next_page.'" title="Next Page">»</a> ';
  }
}

if (($total_pages > 5) && ($current_page == ($total_pages-1))) {
  for ($i=($current_page-3);$i<=$current_page;$i++) {
    if ($i == $current_page) {
      $return .= ' '.$i.' |';
    } else {
      $return .= ' <a href="'.$url.'?page='.$i.'" title="Page '.$i.'">'.$i.'</a> |';
    }
  }
  for ($i=$current_page+1;$i<=$total_pages;$i++) {
    if ($current_page >= $total_pages) {
      break;
    } else {
      $return .= ' <a href="'.$url.'?page='.$i.'" title="Page '.$i.'">'.$i.'</a> |';
    }
  }
  if ($current_page < $total_pages) {
    $return .= ' <a href="'.$url.'?page='.$next_page.'" title="Next Page">»</a> ';
  }
}

if (($total_pages > 5) && ($current_page == $total_pages)) {
  for ($i=($current_page-4);$i<=$current_page;$i++) {
    if ($i == $current_page) {
      $return .= ' '.$i.' |';
    } else {
      $return .= ' <a href="'.$url.'?page='.$i.'" title="Page '.$i.'">'.$i.'</a> |';
    }
  }
  for ($i=$current_page+1;$i<=$total_pages;$i++) {
    if ($current_page >= $total_pages) {
      break;
    } else {
      $return .= ' <a href="'.$url.'?page='.$i.'" title="Page '.$i.'">'.$i.'</a> |';
    }
  }
}

return $return;

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.