SoFunction
Updated on 2025-04-13

Specially written for beginners in combination with smarty class page 3/3



<?php 
//  Page class, just call the functions below the class when using it.
class Page_class 

//Total number of posts
 private $count = 0; 
//How many lines are displayed per page
 private $rows; 
//How many page numbers will be displayed on each page?
 private $link_num; 
//How many pages are there in total
 private $pages = 0; 
//Current page
 private $current_page; 
//Start record
 private $start = 0; 
//Previous page
 private $prve = 0; 
//Next page
 private $next = 0; 
//Digital link
 private $links = array(); 
//Return value
 public $return_rs = array(); 
 public function __construct( $count, $current_page = 1, $rows = 10, $link_num = 7 ) 
 { 
//Get the total number of incoming posts
  $this->count = intval( $count ); 
//Get the current page
  $this->current_page = intval( $current_page ); 
//How many rows are displayed
  $this->rows = intval( $rows ); 
//How many page numbers will be displayed on each page?
  $this->link_num = $link_num; 
//Call the method to calculate the number of pages
  $this->count_page(); 
//Call the method to return the page number
  $this->return_links(); 
//Return value
  $this->return_rs = array( 
  'rows' => $this->rows, 
  'prve' => $this->prve, 
  'next' => $this->next, 
  'pages' => $this->pages, 
  'start' => $this->start, 
  'count' => $this->count, 
  'links' => $this->links, 
  'current_page' => $this->current_page 
  ); 
 } 

 public function __destruct() 
 { 

 } 
//Calculate the number of pages
 private function count_page() 
 { 
//Calculate how many pages are there
  @$this->pages = ceil( $this->count / $this->rows ); 
//If the current page is greater than the maximum number of pages, make it equal to the maximum number of pages; if the current page is less than 1, make it equal to 1
  $this->current_page > $this->pages ? $this->current_page = $this->pages : true ; 
  $this->current_page < 1 ? $this->current_page = 1 : true; 
//Calculate the number of start records of the query
  $this->start = ( $this->current_page - 1 ) * $this->rows; 
 } 
//The function to return the page jump page number
 private function return_links() 
 { 
//Divide the current page by the number of pages to get which "big page" is currently
  $start_s = floor( $this->current_page / $this->link_num ); 
//If the current page number is exactly divisible to display the number of pages, you should subtract $start_s by one, because imagine if it is currently page 7
//The number of pages displayed is also 7, then $start_s=1, which means that it has reached the second "big page", but in fact it should still be
//The first "big page"
  ( $this->current_page % $this->link_num ) == 0 ? $start_s-- : true; 
// Calculate the current "big page" start page number, the algorithm is (current "big page" * number of pages displayed) + 1; for example, 0*7+1=1, 1*7+1=8, 2*7+1=15
  $start_page = ( $start_s * $this->link_num ) + 1; 
//Previous page
  $this->prve = $start_page - 1; 
//Next big page
  $this->next = $start_page + $this->link_num; 
//Start loop to calculate the small page number in the current large page
  for ( $i=0; $i < $this->link_num; $i++ ) 
  { 
//If the next page number has exceeded the total number of pages, it means that it should be stopped.
   if ( $start_page + $i > $this->pages ) 
   { 
    break; 
   } 
//Record the page number in the $this->links_arr array
   $this->links[] = $start_page + $i; 
  } 
 } 

function m_page( $count, $current_page = 1, $rows = 10, $link_num = 7 ) 

 $page = new Page_class( $count, $current_page, $rows, $link_num ); 
 return $page->return_rs; 

?>