A general method to implement data paging in PHP
Release: 2016-07-25 08:45:27
Original
1019 people have browsed it
- 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 parameter in the url to add the new page parameter
- $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; //The left and right offset of the page number
- $navs='';
- if($pages != 0){
- if($page!=1){
- $navs.="Homepage "; //First page
- $navs.="Previous page"; //Previous page
- } else {
- $navs .= "Homepage ";
- $navs .= "Previous page";
- }
- if($pages>$page_len)
- {
- //If the current page is less than or equal to the left Shift
- 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’s right offset exceeds 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.=" < ;a href="".$url."page=".($page+1)."">Next page ";//Next page
- $navs.="Last page"; //Last page
- } else {
- $navs .= "Next page";
- $navs .= "Last page";
- }
- echo "$navs";
- }
- }
Copy code
|
Pagination, PHP
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