SoFunction
Updated on 2025-03-09

Complete example of page paging class encapsulated by php

This article example describes the page pagination class encapsulated by php. Share it for your reference, as follows:

Class files:

<?php
  //Pagination tool class  class Page{
    /*
      * Get the pagination string
      * @param1 string $uri, the script url to be requested by pagination
      * @param3 int $counts, total number of records
      * @param4 int $length, the number of records displayed per page
      * @param5 int $page = 1, current page number
      * @return string, with a tag, you can click the string to initiate the request
     */
    public static function getPageStr($uri,$counts,$length,$page = 1){
      //Construct a clickable string      //Get the string displayed by the data      $pagecount = ceil($counts/$length);        //Total page count      $str_info = "There are currently a total of{$counts}Record,Displayed per page{$length}Record,Total{$pagecount}Page,Currently the first{$page}Page";
      //Generate operationable connection: Home Previous Page Next Page Last Page      //Check the page numbers of the previous page and the next page      $prev = ($page <= 1) ? 1 : $page - 1;
      $next = ($page >= $pagecount) ? $pagecount : $page + 1;
      $str_click = <<<END
        <a href="{$uri}?page=1">首Page</a>
        <a href="{$uri}?page={$prev}">上一Page</a>
        <a href="{$uri}?page={$next}">下一Page</a>
        <a href="{$uri}?page={$pagecount}">末Page</a>
END;
      //Page strings according to page number      $str_number = '';
      for($i = 1;$i <= $pagecount;$i++){
        $str_number .= "<a href='{$uri}?page={$i}'>{$i}</a> ";
      }
      //Page string of drop-down box: use js onchang event to change the href of the current script      $str_select = "<select onchange=\"='{$uri}?page='+\">";
      //Put all page numbers into option      for($i = 1;$i <= $pagecount;$i++){
        if($i == $page)
          $str_select .= "<option value='{$i}' selected='selected'>{$i}</option>";
        else
          $str_select .= "<option value='{$i}'>{$i}</option>";
      }
      $str_select .= "</select>";
      //Return value      return $str_info . $str_click . $str_number . $str_select;
    }
}

For more information about PHP related content, please check out the topic of this site:PHP+ajax tips and application summary》、《Summary of PHP operations and operator usage》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《Summary of php operation office documentation skills (including word, excel, access, ppt)》、《Summary of the usage of php date and time》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.