How to implement paging of arrays in PHP

墨辰丷
Release: 2023-03-28 07:32:01
Original
1786 people have browsed it

This article mainly introduces the PHP implementation of array paging processing, and analyzes the definition and usage skills of the PHP-encapsulated array paging class in the form of examples. Friends in need can refer to it

The details are as follows:

Recently I have used array data to paginate. I have sorted it out here. The specific code is as follows:

<?php
class PaginationArray{
 public $pageArray=array(); //数组
 public $pageSize=10; //每页显示记录数
 public $current= 1; //当前页
 private $total=0; //总页数
 private $prev=0; //上一页
 private $next=0; //下一页
 public $argumetsOther=&#39;&#39;;//设置参数
 function __construct($array=array(),$pageSize=10,$current=1){
 $this->pageArray=$array;
 $this->pageSize=$pageSize;
 $this->current=$current; 
 }
 /*通过数组进行初始化
 * 
 * 数组为关联数组,参数索引为pageArray,pageSize,current
 * 
 */
 function setArguments($arr){
 if (is_array($arr)){
  $this->pageArray=$arr[&#39;pageArray&#39;];
  $this->pageSize=$arr[&#39;pageSize&#39;];
  $this->current=$arr[&#39;current&#39;];
 }else{
  return ;
 }
 }
 //返回链接
 function page(){
 $_return=array();
 /*calculator*/
 $this->total=ceil(Count($this->pageArray)/$this->pageSize);
 $this->prev=(($this->current-1)<= 0 ? "1":($this->current-1));
 $this->next=(($this->current+1)>=$this->total ? $this->total:$this->current+1);
 $current=($this->current>($this->total)?($this->total):$this->current);
 $start=($this->current-1)*$this->pageSize;
 $arrleng=count($this->pageArray);
 for($i=$start;$i<($start+$this->pageSize);$i++){
  if($i >= $arrleng)break;
  array_push($_return,$this->pageArray[$i]);
 }
 $pagearray["source"]=$_return;
 $pagearray["links"]=$this->linkStyle(2);
 return $pagearray;
 }
 //链接的样式
 private function linkStyle($number=1){
 $linkStyle=&#39;&#39;;
 switch ($number){
  case 1:
  $linkStyle="<a href=\"?page=1\">first</a> <a href=\"?page={$this->prev}\">prev</a> <a href=\"?page={$this->next}\">next</a> <a href=\"?page={$this->total}\">end</a>";
  break;
  case 2:
  $linkStyle="<a href=\"?page=1&{$this->argumetsOther}\">首页</a> <a href=\"?page={$this->prev}&{$this->argumetsOther}\">上一页</a> <a href=\"?page={$this->next}&{$this->argumetsOther}\">下一页</a> <a href=\"?page={$this->total}&{$this->argumetsOther}\">尾页</a>";
  break;
 }
 return $linkStyle;
 }
}
//调用的实例
/*
header(&#39;Content-Type: text/html;charset=utf-8&#39;);
$array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");
$page= isset($_GET[&#39;page&#39;])? $_GET[&#39;page&#39;] : 1 ;
$arrayPage = new PaginationArray($array,"5",$page);
$r = $arrayPage->page();
foreach($r["source"] as $s){
 echo $s.&#39;<br />&#39;;
}
echo $r["links"];
*/
?>
Copy after login

The above is the entire article Content, I hope it will be helpful to everyone’s study.


Related recommendations:

php redis implements functions such as registration, deletion, editing, paging, login, and follow Method

Ajax no refreshPagingPerformance optimization method

php simple implementationPagingQuery method

The above is the detailed content of How to implement paging of arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!