Jump to content

Pagination Several Pages


dweb77

Recommended Posts

Is there an easy way to adjust this code for pagination that would handle say 100 pages such that the display is only showing 5 or 10 pages at a time then advances to the next set of pages with a next arrow, or shifts the pages to be 6-10 (if the first set was 1-5)? I have the code below which works, but it shows all pages, the next and previous buttons taking the user to each page. The code below has the variables for $start, $display, and $num_pages defined earlier in the script.

 

Thanks.

 


if ($num_pages > 1) {

$current_page = ($start/$display) + 1;

if ($current_page !=1) {

echo '<font face="Verdana" size="1"><a href="schools.php?s=' . ($start - $display) . '&no problem=' . $num_pages . '&user=' . $user . '" target="_self"><b><</b></a> · </font>';

}

for ($i = 1; $i <=$num_pages; $i++) {
if ($i !=$current_page) {

echo '<font face="Verdana" size="2"><a href="schools.php?s=' . (($display * ($i - 1))) . '&no problem=' . $num_pages . '&user=' . $user . '" target="_self"> ' . $i . ' </a> · </font>';

} else {

echo '<font face="Verdana" size="2"><b>' . $i . ' · </b></font>';
}
}


if ($current_page != $num_pages) {

echo ' <font face="Verdana" size="2"><a href="schools.php?s=' . ($start + $display) . '&no problem=' . $num_pages . '&user=' . $user . '" target="_self"><b>></b></a></font>';

}

 

Link to comment
https://forums.phpfreaks.com/topic/62570-pagination-several-pages/
Share on other sites

Short answer is no, it's not easy :)  Here is my code:

 

<?php
function get_page_numbers($query_count, $rows_per_page, &$page_num, &$num_pages, &$first_link, &$prev_link, &$next_link, &$last_link, &$prevnext) {
  # Page number related calculations and query addition
  # $query_count is the number of items to be paged through
  $num_pages = (($query_count - ($query_count % $rows_per_page))
        / $rows_per_page);
  if ($query_count % $rows_per_page != 0) $num_pages++;
  if (!is_numeric($page_num)) $page_num = 0;
  if ($page_num > $num_pages - 1) { $page_num = 0; }

  # Information for displaying page 1 2 3 4 ... links
  if ($num_pages > 10) {
    if ($page_num >= 5) {
        $prev_link = $page_num - 5;
        $first_link = 0;
        for ($i = 0; $i < 9 && $page_num + $i - 4 < $num_pages; $i++) {
            $prevnext[$i] = $page_num + $i - 4;
        }
    } else {
        $prev_link = -1;
        $first_link = -1;
    }
    if (($num_pages - $page_num) >= 5) {
        $next_link = $page_num + 5;
        $last_link = $num_pages - 1;
        if ($page_num < 5) {
            for ($i = 0; $i < 9; $i++) {
                $prevnext[$i] = $i;
            }
        }
        # If $page_num >= 5, then list was populated earlier
    } else {
        $next_link = -1;
        $last_link = -1;
    }
  }
  if ($num_pages <= 10) {
        $prev_link = $next_link = -1;
        $first_link = $last_link = -1;
        for ($i = 0; $i < $num_pages; $i++) {
                $prevnext[$i] = $i;
        }
  }
}
?>

Archived

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

×
×
  • 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.