1024programmer PHP PHP implementation of paging: text paging and number paging, _PHP tutorial

PHP implementation of paging: text paging and number paging, _PHP tutorial

PHP implements paging: text paging and number paging,


Source: http://www.ido321.com/1086.html

Recently, paging is used in projects. The paging function is a frequently used function, so it is encapsulated in the form of a function.

//Paging and packaging
 /**
 * $pageType paging type 1 is digital paging 2 is text paging
 * You can pass $pageTotal, $page, $total and other data as parameters, or as global variables in paging (recommended)
 */
 function paging($pageType)
 {
     global $pageTotal,$page,$total;
     if($pageType == 1)
     {
         echo '

'; echo'

    '; for($i=0; $i <$pageTotal; $i++) { if($page == ($i+1)) { echo '
  • .($i+1).'" class="selected">'.($i+1). '
  • '
    ; } else { echo '
  • .($i+1).'">'.($i+1).'
  • '
    ; } } echo'
'
; echo'

'
; } else if($pageType == 2) { echo '

'; echo '

    '; echo '
  • '.$page.'/'.$pageTotal.'page |
  • '
    ; echo '
  • Total'.$total .' members |
  • '
    ; //First page if($page == 1) { echo '
  • Home |
  • '
    ; echo '
  • Previous page |
  • '
    ; } else { // $_SERVER["SCRIPT_NAME"] gets the current script name for easy transplantation //You can also customize constants, the constant values ​​​​are consistent with the script file name echo '
  • .$_SERVER["SCRIPT_NAME"].'">Home |
  • '
    ; echo '
  • .$_SERVER["SCRIPT_NAME"].'?page='.($page - 1) .'">Previous page|
  • '
    ; } //Last page if($page == $pageTotal) { echo '
  • Next page |
  • '
    ; echo '
  • Last page |
  • '
    ; } else { echo '
  • .$_SERVER["SCRIPT_NAME"].'?page='.($page + 1) .'">Next page|
  • '
    ; echo '
  • .$_SERVER["SCRIPT_NAME"].'?page='.($pageTotal).'">Last page |
  • '
    ; } echo '
'
; echo '

'
; } }

Parameter explanation:

$pageTotal is the total number of pages, $page is the current page, $total is the total number of data obtained from the database;

For simplicity, all parameters are encapsulated

//Paging parameter packaging
 /**
 * $sql can be a sql statement to obtain the total number of data
 * $size Number of items displayed on each page
 */
 function pageParam($sql,$size)
 {
     //Set all involved parameters into global variables
     // $pagestart Where to start a page
     // $total total number of records $page a certain page $pageTotal total number of pages
     global $pagestart,$pagesize,$total,$page,$pageTotal;
     $pagesize = $size;
     //Get the total number of data
     $total = mysql_num_rows(queryDB($sql));

     // Error handling, first determine whether it exists
     if(isset($_GET['page']))
     {
         //A specific page
         $page = $_GET['page'];
         // Determine whether it is empty (0 is empty)/less than 0/whether it is a number
         if(empty($page) || $page <0 || !is_numeric($page))
         {
             $page = 1;
         }
         else
         {
             $page = intval($page); //Rounding to prevent decimals
         }
        
     }
     else
     {
         //Initialize page 1
         $page = 1;
     }

     //Clear database
     if($total == 0)
     {
         //Set to 1
         $pageTotal = 1;
     }
     else
     {
         //The total number of pages in the pagination (rounded up)
         $pageTotal = ceil($total / $pagesize);
     }

     //The number of pages is greater than the total page number $total
     if($page > $pageTotal)
     {
         $page = $pageTotal;
     }
     //The current page starts from a certain record
     $pagestart = ($page - 1) * $pagesize;
 }

Parameter explanation:

$pagestart is when the page starts from a certain record, $pagesize is the number of records displayed on each page

In use, call pageParam first, then call paging

/**
 * The first sql statement that can obtain the total number of data
 * The second number of items displayed on each page
 */
 pageParam("select userid from user",2);
<?php
     //Paging type 1 is digital paging 2 is text paging
       paging(2);
 ?> 

The calling position is selected according to the specific situation. The text is paginated as follows:

<?php
        //Paging type 1 is digital paging 2 is text paging
         paging(1);
  ?> 

The digital pagination is as follows:

The style can be adjusted by itself.

Next article: A small example of Google Maps API displaying a map




http://www.bkjia.com/PHPjc/897990.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/897990.htmlTechArticlePHP implements paging: text paging and number paging, source: http://www.ido321.com/1086.html Recently, paging is used in projects. The paging function is a frequently used function, so…

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/php-implementation-of-paging-text-paging-and-number-paging-_php-tutorial-3/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索