PHP array-based paging implementation

Source: Internet
Author: User

On the array of paging function, the advantage of using an array for paging is convenient for the Union of multiple table query, just want to put the results of the query in the array can be the following is the function of the array paging, function Page_array for the array of pagination, function Show_array for the operation and display of the paging function, Need to be used in conjunction with. Two functions are linked by a global variable $countpage, which is used to track the total number of pages.

<?PHP/** Number of Components page function core function Array_slice * Before using this function, all the data in the database must be queried in a certain order to be stored in the array * $count how many data per page * $page Current page * $array Query out all arrays * Order 0-Unchanged 1-Reverse order*/     functionPage_array ($count,$page,$array,$order){      Global $countpage;#Set Global Variables    $page=(Empty($page))?‘ 1 ':$page;#determines whether the current page is empty if it is empty, it is represented as the first page       $start=($page-1) *$count;#calculate the starting position of each page    if($order==1){        $array=Array_reverse($array); }         $totals=Count($array); $countpage=Ceil($totals/$count);#calculate the total number of pages    $pagedata=Array(); $pagedata=Array_slice($array,$start,$count); return $pagedata;#return query Data}  /** * pagination and display function * $countpage global variable, write * $url current URL*/  functionShow_array ($countpage,$url){       $page=Empty($_get[' page '])? 1:$_get[' Page ']; if($page> 1){          $uppage=$page-1; }Else{          $uppage=1; }         if($page<$countpage){          $nextpage=$page+1; }Else{              $nextpage=$countpage; }                   $str= ' <div style= ' border:1px; width:300px; height:30px; Color: #9999CC ' > '; $str. = "<span> Total {$countpage} page/Section {$page} page </span> "; $str. = "<span><a href="$url? page=1 ' > Home </a></span> "; $str. = "<span><a href="$url? page={$uppage} ' > previous page </a></span> '; $str. = "<span><a href="$url? page={$nextpage} ' > Next </a></span> '; $str. = "<span><a href="$url? page={$countpage} ' > Last </a></span> '; $str. = ' </div> '; return $str; }  ?>

PHP array-based paging implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.