PHP based on array implementation of pagination function Example _php skill

Source: Internet
Author: User
Tags php programming

Paging function is a very common function of PHP programming, unlike in the past, today This article describes PHP is based on the array implementation of the paging function.

About the paging function of an array, the advantage of using an array for paging is that it is convenient to combine multiple table queries, just put the results of the query in an array. The following is the function of array paging, function Page_array for array paging, function Show_array for paging function operation and display, Need to be used in conjunction. Two functions are contacted through global variable $countpage, which is used to track the total number of pages.

The specific example code is as follows:

<?php/** * Number of Components page function core function Array_slice * Use this function to first of all the data in the database in a certain order to query out into the array * $count how many pieces of data per page * $page Current page * $arra Y query out all arrays * Order 0-Invariant 1-Reverse sequence/function Page_array ($count, $page, $array, $order) {global $countpage; #定全局变量 $ Page= (Empty ($page))? ' 1 ': $page; #判断当前页面是否为空 If NULL is the first page $start = ($page-1) * $COUNT;
  #计算每次分页的开始位置 if ($order ==1) {$array =array_reverse ($array); 
  $totals =count ($array); $countpage =ceil ($totals/$count);
 #计算总页面数 $pagedata =array ();
  $pagedata =array_slice ($array, $start, $count); return $pagedata; #返回查询数据}/** * Paging and display function * $countpage global variable, write * $url current URL/function Show_array ($countpage, $url) {$page =empty ($_ge
 t[' 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/{$page} page &LT;/SPAN&Gt; ";
 $str. = "<span><a href= ' $url" page=1 ' > Home </a></span> ";
 $str. = "<span><a href= ' $url? page={$uppage} ' > Prev </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;

 }?>

I hope that the example described in this article will play a reference role in the PHP program design for everyone.

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.