PHP paging function code sharing
Release: 2016-07-25 08:51:12
Original
908 people have browsed it
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
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31