For details, see http://qxblog.sinaapp.com/?p=100
- //$count is the total number of entries, $page is the current page number, $page_size is the number of entries displayed on each page
- function show_page($count,$page, $page_size)
- {
- $page_count = ceil($count/$page_size); //Calculate the total number of pages
-
- $init=1;
- $page_len=7;
- $max_p=$page_count;
- $pages= $page_count;
-
- //Judge the current page number
- $page=(empty($page)||$page<0)?1:$page;
- //Get the current page url
- $url = $_SERVER['REQUEST_URI' ];
- //Remove the original page parameters in the url to add new page parameters
- $parsedurl=parse_url($url);
- $url_query = isset($parsedurl['query']) ? $parsedurl['query'] :'';
- if($url_query != ''){
- $url_query = preg_replace("/(^|&)page=$page/",'',$url_query);
- $url = str_replace($parsedurl ['query'],$url_query,$url);
- if($url_query != ''){
- $url .= '&';
- }
- } else {
- $url .= '?';
- }
-
- //Paging function code
- $page_len = ($page_len%2)?$page_len:$page_len+1; //Number of page numbers
- $pageoffset = ($page_len-1)/2; //About the number of page numbers Offset
- $navs='';
- if($pages != 0){
- if($page!=1){
- $navs.="Homepage "; //First page
- $navs.=""; //Previous page
- } else {
- $navs .= "Homepage";
- $navs .= "< ;span class='disabled'>Previous page";
- }
- if($pages>$page_len)
- {
- //If the current page is less than or equal to the left offset
- if($page<=$ pageoffset){
- $init=1;
- $max_p = $page_len;
- }
- else //If the current page is greater than the left offset
- {
- //If the current page number right offset exceeds the maximum number of pages
- if($page+ $pageoffset>=$pages+1){
- $init = $pages-$page_len+1;
- }
- else
- {
- //Calculation when both left and right offsets exist
- $init = $page-$pageoffset;
- $max_p = $page+$pageoffset;
- }
- }
- }
- for($i=$init;$i<=$max_p;$i++)
- {
- if($i==$page){$navs.= "".$i.'';}
- else {$navs.=" ".$i."";}
- }
- if($page!=$pages)
- {
- $navs.=" Next page ";//Next page
- $navs.="Last page"; //Last page
- } else {
- $navs .= "Next page
- $navs .= "Last page";
- }
- echo "$navs";
- }
- }
Copy code
|