php分頁程序paginator程序代碼
May 25, 2016 pm 04:54 PM本文章來給大家介紹一個paginator的php分頁程序代碼,有需要的朋友可進(jìn)入?yún)⒖肌?/p>
<?php class paginator { function __construct() { } static function render($page_type, array $args = array()) { $html = ''; switch ($page_type) { case '1': $html = page_1($channel_code, $page_num, $cur_page, $per_group, $base_url, $n, $v); break; case '2': $html = fpage($row_num, $pagesize, $page, $url); break; case '3': break; default: $html = page($num, $perpage, $curpage, $mpurl, $hiddenFrom = ''); } return $html; } //函數(shù)1:根據(jù)總頁數(shù),當(dāng)前頁,和頁分組及url產(chǎn)生分頁導(dǎo)航 function page_1($channel_code, $page_num, $cur_page, $per_group, $base_url, $n, $v) { // 分頁函數(shù)參數(shù)列表(有多少頁,當(dāng)前頁,每頁多少個 ,鏈接地址) $base_url.= strpos($base_url, '?') ? '&' : '?'; //如果有?,則添加&,否則加? $page_val = ""; if ($page_num < 1) { return $page_val; //如果小于1,返回空 } elseif ($page_num == 1) { return $page_val = 1; //如果等于1,返回1 } //分組 $group_ok = !(($page_num - 1) % ($per_group - 1)); //沒有余數(shù) // 計算一共有多少組 if ($group_ok) { $group_num = ($page_num - 1) / ($per_group - 1); } else { $group_num = ceil(($page_num - 1) / ($per_group - 1)); } //判斷當(dāng)前頁是否為一組的最后一個 //組數(shù) $is_last_page = !(($cur_page - 1) % ($per_group - 1)); //計算當(dāng)前在的組數(shù) if ($is_last_page) { $cur_group = ($cur_page - 1) / ($per_group - 1) + 1; } else { $cur_group = ceil(($cur_page - 1) / ($per_group - 1)); } // 計算顯示的起始頁 ,這個關(guān)鍵 $from = ($cur_group - 1) * ($per_group - 1) + 1; $from = $cur_group <= 1 ? 1 : $from; $to = $from + $per_group; $to = $to > $page_num ? $page_num + 1 : $to; for ($a = $from; $a < $to; $a++) { if ($a != $cur_page) { //不是當(dāng)前頁 $page_val.= '<li><a href ="' . $base_url . 'channel=' . $channel_code . '&n=' . $n . '&v=' . $v . '&page=' . $a . '">' . $a . '</a></li>'; } else { //當(dāng)前頁 $page_val.= '<li class="current">' . $a . '</li>'; } } //上一頁下一頁首頁尾頁 $next = $cur_page + 1; //當(dāng)前頁+1 $prev = $cur_page - 1; //當(dāng)前頁-1 if ($cur_page != $page_num) { //當(dāng)前頁不是最后一頁 $page_val.= '<span><a href ="' . $base_url . 'channel=' . $channel_code . '&n=' . $n . '&v=' . $v . '&page=' . $next . '">' . '下一頁' . '</a></span>'; $page_val.= '<span><a href ="' . $base_url . 'channel=' . $channel_code . '&n=' . $n . '&v=' . $v . '&page=' . $page_num . '">' . '>>' . '</a></span>'; //尾頁 } if ($cur_page != 1) { //不是第一頁,加上前面的內(nèi)容 $page_val = '<span><a href ="' . $base_url . 'channel=' . $channel_code . '&n=' . $n . '&v=' . $v . '&page=' . $prev . '">' . '上一頁' . '</a></span>' . $page_val; $page_val = '<span><a href ="' . $base_url . 'channel=' . $channel_code . '&n=' . $n . '&v=' . $v . '&page=1">' . '<<' . '</a></span>' . $page_val; //首頁 } $page_val = '<ul class="page_nav">' . $page_val . '</ul>'; return $page_val; } // 函數(shù)2:根據(jù)記錄數(shù),頁列清數(shù),當(dāng)前頁,url年成分頁 function fpage($row_num, $pagesize, $page, $url) { //$page,當(dāng)前頁;$row_num記錄總數(shù);$pagesize:每頁記錄數(shù);$url記錄頁; //如果,如果有作數(shù),則取加1 $page_count = $row_num % $pagesize ? intval($row_num / $pagesize) + 1 : intval($row_num / $pagesize); if ($page_count == 1) { $page_string = "第一頁|上一頁|下一頁|尾頁"; } else { //如果不止一頁 if ($page == 1) { //第一頁顯示 $page_string = " <a href=" . $url . "?page=1>第一頁 </a>|上一頁| <a href=" . $url . "?page=" . ($page + 1) . ">下一頁 </a>| <a href=" . $url . "?page=" . $page_count . ">尾頁 </a>"; } if (($page > 1) and ($page < $page_count)) { $page_string = " <a href=" . $url . "?page=1>第一頁 </a>| <a href=" . $url . "?page=" . ($page - 1) . ">上一頁 </a>| <a href=" . $url . "?page=" . ($page + 1) . ">下一頁 </a>| <a href=" . $url . "?page=" . $page_count . ">尾頁 </a>"; } if ($page == $page_count) { $page_string = " <a href=" . $url . "?page=1>第一頁 </a>| <a href=" . $url . "?page=" . ($page - 1) . ">上一頁 </a>|下一頁| <a href=" . $url . "?page=" . $page_count . ">尾頁 </a>"; } } $page_str = $page_string . ("," . $page . "/" . $page_count . "頁"); echo " <br> <p align=center>" . $page_str . " </p>"; } /** * 分頁函數(shù) * * @param int $num:記錄總數(shù) * @param int $perpage:每頁記錄 * @param int $curpage:當(dāng)前頁 * @param string $mpurl:路徑url * @param unknown_type $hiddenFrom * @return string */ function page($num, $perpage, $curpage, $mpurl, $hiddenFrom = '') { $multipage = ''; //恢復(fù) $mpurl.= strpos($mpurl, '?') ? '&' : '?'; if ($num > $perpage) { $page = 10; $offset = 2; $pages = @ceil($num / $perpage); //總頁數(shù) if ($page > $pages) { $from = 1; $to = $pages; } else { //如果總頁數(shù)>10 $from = $curpage - $offset; //當(dāng)前頁-2 $to = $from + $page - 1; //當(dāng)前頁+10-3 if ($from < 1) { $to = $curpage + 1 - $from; $from = 1; if ($to - $from < $page) { $to = $page; } } elseif ($to > $pages) { $from = $pages - $page + 1; $to = $pages; } } if ($hiddenFrom) { $multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="#" ' . js_page($hiddenFrom, 1) . '>首頁</a> ' : '') . ($curpage > 1 ? ' <a href="#" ' . js_page($hiddenFrom, $curpage - 1) . '>‹‹上一頁</a> ' : ''); for ($i = $from; $i <= $to; $i++) { $multipage.= $i == $curpage ? '<span>' . $i . '</span> ' : '<a href="#" ' . js_page($hiddenFrom, $i) . '>' . $i . '</a> '; } $multipage.= ($curpage < $pages ? '<a href="#" ' . js_page($hiddenFrom, $curpage + 1) . '>下一頁››</a>' : '') . ($to < $pages ? '<a href="#" ' . js_page($hiddenFrom, $pages) . '> 末頁</a>' : ''); $multipage = $multipage ? '<div class="list-page">' . $multipage . '</div>' : ''; } else { $multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="' . $mpurl . 'page=1">首頁</a> ' : '') . ($curpage > 1 ? ' <a href="' . $mpurl . 'page=' . ($curpage - 1) . '">‹‹上一頁</a> ' : ''); for ($i = $from; $i <= $to; $i++) { $multipage.= $i == $curpage ? '<span>' . $i . '</span> ' : '<a href="' . $mpurl . 'page=' . $i . '">' . $i . '</a> '; } $multipage.= ($curpage < $pages ? '<a href="' . $mpurl . 'page=' . ($curpage + 1) . '">下一頁››</a>' : '') . ($to < $pages ? '<a href="' . $mpurl . 'page=' . $pages . '"> 末頁</a>' : ''); $multipage = $multipage ? '<div class="list-page">' . $multipage . '</div>' : ''; } } return $multipage; } //暫不用 function page_get_start($page, $ppp, $totalnum) { $totalpage = ceil($totalnum / $ppp); $page = max(1, min($totalpage, intval($page))); return ($page - 1) * $ppp; } //暫不用 function js_page($hiddenFrom, $page) { if ($hiddenFrom) { return ' onclick=javascript:$("#page").val("' . $page . '");$("#' . $hiddenFrom . '").submit(); '; } } } ?>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PHP development: How to implement table data sorting and paging functions In web development, processing large amounts of data is a common task. For tables that need to display a large amount of data, it is usually necessary to implement data sorting and paging functions to provide a good user experience and optimize system performance. This article will introduce how to use PHP to implement the sorting and paging functions of table data, and give specific code examples. The sorting function implements the sorting function in the table, allowing users to sort in ascending or descending order according to different fields. The following is an implementation form

CakePHP is a powerful PHP framework that provides developers with many useful tools and features. One of them is pagination, which helps us divide large amounts of data into several pages, making browsing and manipulation easier. By default, CakePHP provides some basic pagination methods, but sometimes you may need to create some custom pagination methods. This article will show you how to create custom pagination in CakePHP. Step 1: Create a custom pagination class First, we need to create a custom pagination class. this

As data continues to grow, tabular display becomes more difficult. Most of the time, the amount of data in a table is so large that it becomes slow to load and users need to constantly browse the page to find the data they want. This article will introduce how to use JavaScript to realize paginated display of table data, making it easier for users to find the data they want. 1. Dynamically create tables. In order to make the paging function more controllable, tables need to be created dynamically. In the HTML page, add a table element similar to the one below.

How to use JavaScript to implement table paging function? With the development of the Internet, more and more websites use tables to display data. In some cases where the amount of data is large, the data needs to be displayed in pages to improve user experience. This article will introduce how to use JavaScript to implement table paging function and provide specific code examples. 1. HTML structure First, we need to prepare an HTML structure to host tables and paging buttons. We can use <tab

How to use PHP and CGI to implement the paging and sorting functions of a website Preface Paging and sorting of a website are very common and important functions, especially for the display and processing of large amounts of data. In this article, we will use PHP and CGI (Common Gateway Interface) technology to implement the paging and sorting functions of the website, with corresponding code examples. 1. Implementation of paging function The paging function mainly obtains a specific range of data by querying the database, and displays the data according to the specified page number and display quantity per page. The following are the steps to implement paging functionality

Vue is a popular JavaScript framework for building user interfaces. In the development of Vue technology, implementing paging function is a common requirement. This article will introduce how to use Vue to implement paging function and provide specific code examples. Before we start, we need to prepare some basic knowledge in advance. First, we need to understand the basic concepts and syntax of Vue. Secondly, we need to know how to use Vue components to build our application. Before we start, we need to install a paging plug-in in the Vue project,

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis
