PHP: Pagination
// Usage:
// print get_page_nav($page_current, $page_total);
/** * Creates links to pages. * * @param integer $page_current The current page number. * @param integer $page_total The total number of pages. * @return string HTML-code. */ function get_page_nav($page_current, $page_total) { $ar_pages = array(); /* HTML-tag to select the current page */ $str_tag = 'strong'; /* Language strings */ $str_page_prev = 'Предыдущая'; $str_page_next = 'Следующая'; /* String placed between the first and last page numbers */ $str_more = '..'; /* String to separate page numbers */ $str_d = $this->sys['char_split_pages']; /* URL for paging */ $str_url = 'index.php?book='.$this->gv['book'].'&page=%d'; /* The number of links to pages displayed before and after the current page. 1 2 (3) 1 2 */ $int_max = 2; $cnt_new = $page_current; $cnt_max = $page_current + $int_max; $cnt_min = $page_current - $int_max; /* Fix the maximum number of pages */ if ($cnt_max > $page_total) { $cnt_max = $page_total; } /* First page */ if ($cnt_min > 1) { $ar_pages[] = '<a href="'.sprintf($str_url, 1).'">1</a>'; $ar_pages[] = $str_more; } /* For each page number */ for ($i = 1; $i <= $page_total; $i++) { if ( ($i >= $cnt_min && $i <= $cnt_max) ) { if ($i == $page_current) { $ar_pages[] = '<a href="'.sprintf($str_url, $i).'"><'.$str_tag.'>'.$i.'</'.$str_tag.'></a>'; } else { $ar_pages[] = '<a href="'.sprintf($str_url, $i).'">'.$i.'</a>'; } } $cnt_new++; } /* The last page */ if ($cnt_max > 1 && ($page_current + $int_max) < $page_total) { $ar_pages[] = $str_more; $ar_pages[] = '<a href="'.sprintf($str_url, $page_total).'">'.$page_total.'</a>'; } /* Links to Next/Prev pages */ if ($page_current > 1) { $ar_pages[] = '<a href="'.sprintf($str_url, ($page_current - 1) ).'">'.$str_page_prev.'</a>'; } if ($page_current < $page_total) { $ar_pages[] = '<a href="'.sprintf($str_url, ($page_current + 1) ).'">'.$str_page_next.'</a>'; } return implode($str_d, $ar_pages); }