A simple generic paging class written by a user
Simple paging class written by php for personal learning, with strong versatility
- /**
- * Simple paging class
- * @ Author: phpprince
- * @ QQ: 8923052.
- * @ Date: 21:08:35
- */
- Class Page {
- Protected $ url; // address
- Protected $ allnum; // total number of records
- Protected $ current; // current page number
- Protected $ pagesize; // number of records displayed on each page
- Protected $ postfix; // suffix
- Protected $ style; // A total of three display styles. 1 2 3 represents the first 5, the last 4, the last 3, and the last 4, respectively.
- Public function _ construct ($ url, $ allnum, $ current, $ pagesize = 10, $ postfix = '', $ style = 1 ){
- $ This-> url = $ url;
- $ This-> allnum = $ allnum;
- $ This-> current = $ current;
- $ This-> pagesize = $ pagesize;
- $ This-> postfix = $ postfix;
- $ This-> style = $ style;
- }
- // Obtain the total number of pages
- Protected function maxPageNum (){
- $ Max = ceil ($ this-> allnum/$ this-> pagesize );
- // Page number exceeding correction
- If ($ this-> current> $ max ){
- $ This-> current = $ max;
- }
- If ($ this-> current <1 ){
- $ This-> current = 1;
- }
- Return $ max;
- }
- // Obtain the complete html str link on the first page
- Protected function firstUrl (){
- If ($ this-> current! = 1)
- {
- Return 'geturl (1). '"title =" View first Page "> homepage ';
- } Else {
- Return 'homepage ';
- }
- }
- // Obtain the complete html str link of the previous page
- Protected function prevUrl (){
- If ($ this-> current <= 1 ){
- $ Fullurl = 'preview ';
- } Else {
- $ Fullurl = $ this-> url. ($ this-> current-1). $ this-> postfix;
- $ Fullurl = 'preview ';
- }
- Return $ fullurl;
- }
- // Obtain the link of the next page. complete html str
- Protected function nextUrl (){
- If ($ this-> current >=$ this-> maxPageNum ()){
- $ Fullurl = 'next page ';
- } Else {
- $ Fullurl = $ this-> url. ($ this-> current + 1). $ this-> postfix;
- $ Fullurl = 'next page ';
- }
- Return $ fullurl;
- }
- // Obtain the complete html str link on the last page
- Protected function lastUrl (){
- If ($ this-> current >=$ this-> maxPageNum ()){
- $ Fullurl = 'Last page ';
- } Else {
- $ Fullurl = $ this-> url. $ this-> maxPageNum (). $ this-> postfix;
- $ Fullurl = 'Last page ';
- }
- Return $ fullurl;
- }
- // Obtain the complete url of the specified page number
- Protected function getUrl ($ pageno ){
- Return $ this-> url. $ pageno. $ this-> postfix;
- }
- // Specify the display style
- Protected function getStyle (){
- Switch ($ this-> style ){
- Case 1:
- $ Before = 5;
- $ After = 4;
- Break;
- Case 2:
- $ Before = 3;
- $ After = 3;
- Break;
- Case 3:
- $ Before = 4;
- $ After = 4;
- Break;
- Default:
- $ Before = 5;
- $ After = 4;
- }
- Return array ($ before, $ after );
- }
- // Obtain the intermediate URL 1 2 3 4 5 6 7 8 9 10
- Protected function getMiddelUrl (){
- $ Max = $ this-> maxPageNum (); // obtain the total page first to check whether the current page has exceeded the limit.
- $ Current = $ this-> current; // The current page number must be valid to ensure that the following page number ranges are correct.
- // Obtain the current style
- List ($ before, $ after) = $ this-> getStyle ();
- $ Startno = $ current-$ before; // start page number
- $ Endno = $ current + $ after; // end page number
- // To ensure that the output always meets the requirements, when the page is not exceeded, the start page must be added, and vice versa.
- While ($ endno> $ max | $ startno <1 ){
- If ($ endno> $ max) {// ends the page number exceeding the limit
- $ Endno --;
- If ($ startno> 1 ){
- $ Startno --;
- }
- }
- If ($ startno <1) {// the starting page number is out of bounds.
- $ Startno ++;
- If ($ endno <$ max ){
- $ Endno ++;
- }
- }
- }
- $ Str = ''; // used to save the entire html str
- For ($ I = $ startno; $ I <= $ endno; $ I ++ ){
- $ Currenturl = $ this-> getUrl ($ I );
- If ($ I! = $ Current ){
- $ Str. = "{$ I }";
- } Else {
- $ Str. = ''. $ I .'';
- }
- }
- Return $ str;
- }
- // Returns the complete pagination html string
- Public function getPageStr (){
- $ Str ='
'. $ This-> firstUrl (). $ this-> prevUrl ();
- $ Str. = $ this-> getMiddelUrl ();
- $ Str. = $ this-> nextUrl (). $ this-> lastUrl (). 'col '. $ this-> maxPageNum (). 'page '. $ this-> allnum. 'entries';
- Return $ str;
- }
- }
- ?>
|