|
关于数组的分页函数,用数组进行分页的好处是可以方便的进行联合多表查询,只需要将查询的结果放在数组中就可以了以下是数组分页的函数,函数page_array用于数组的分页,函数show_array用于分页函数的操作及显示,需要配合使用.两个函数通过全局变量$countpage发生联系,此变量用于跟踪总页码数.
<?php
/**
* 数组分页函数 核心函数 array_slice
* 用此函数之前要先将数据库里面的所有数据按一定的顺序查询出来存入数组中
* $count 每页多少条数据
* $page 当前第几页
* $array 查询出来的所有数组
* order 0 - 不变 1- 反序
*/
function page_array($count,$page,$array,$order){
global $countpage; #定全局变量
$page=(empty($page))?&#39;1&#39;:$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; #返回查询数据
}
/**
* 分页及显示函数
* $countpage 全局变量,照写
* $url 当前url
*/
function show_array($countpage,$url){
$page=empty($_GET[&#39;page&#39;])?1:$_GET[&#39;page&#39;];
if($page > 1){
$uppage=$page-1;
}else{
$uppage=1;
}
if($page < $countpage){
$nextpage=$page+1;
}else{
$nextpage=$countpage;
}
$str=&#39;<div style=&#34;border:1px; width:300px; height:30px; color:#9999CC&#34;>&#39;;
$str.=&#34;<span>共 {$countpage} 页 / 第 {$page} 页</span>&#34;;
$str.=&#34;<span><a href=&#39;$url?page=1&#39;> 首页 </a></span>&#34;;
$str.=&#34;<span><a href=&#39;$url?page={$uppage}&#39;> 上一页 </a></span>&#34;;
$str.=&#34;<span><a href=&#39;$url?page={$nextpage}&#39;>下一页 </a></span>&#34;;
$str.=&#34;<span><a href=&#39;$url?page={$countpage}&#39;>尾页 </a></span>&#34;;
$str.=&#39;</div>&#39;;
return $str;
}
?>
<?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 ? &#34;1&#34;:($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[&#34;source&#34;]=$_return;
$pagearray[&#34;links&#34;]=$this->linkStyle(2);
return $pagearray;
}
//链接的样式
private function linkStyle($number=1){
$linkStyle=&#39;&#39;;
switch ($number){
case 1:
$linkStyle=&#34;<a href=\&#34;?page=1\&#34;>first</a> <a href=\&#34;?page={$this->prev}\&#34;>prev</a> <a href=\&#34;?page={$this->next}\&#34;>next</a> <a href=\&#34;?page={$this->total}\&#34;>end</a>&#34;;
break;
case 2:
$linkStyle=&#34;<a href=\&#34;?page=1&{$this->argumetsOther}\&#34;>首页</a> <a href=\&#34;?page={$this->prev}&{$this->argumetsOther}\&#34;>上一页</a> <a href=\&#34;?page={$this->next}&{$this->argumetsOther}\&#34;>下一页</a> <a href=\&#34;?page={$this->total}&{$this->argumetsOther}\&#34;>尾页</a>&#34;;
break;
}
return $linkStyle;
}
}
//调用的实例
/*
header(&#39;Content-Type: text/html;charset=utf-8&#39;);
$array=array(&#34;1&#34;,&#34;2&#34;,&#34;3&#34;,&#34;4&#34;,&#34;5&#34;,&#34;6&#34;,&#34;7&#34;,&#34;8&#34;,&#34;9&#34;,&#34;10&#34;,&#34;11&#34;,&#34;12&#34;,&#34;13&#34;,&#34;14&#34;,&#34;15&#34;,&#34;16&#34;,&#34;17&#34;,&#34;18&#34;,&#34;19&#34;,&#34;20&#34;);
$page= isset($_GET[&#39;page&#39;])? $_GET[&#39;page&#39;] : 1 ;
$arrayPage = new PaginationArray($array,&#34;5&#34;,$page);
$r = $arrayPage->page();
foreach($r[&#34;source&#34;] as $s){
echo $s.&#39;<br />&#39;;
}
echo $r[&#34;links&#34;];
*/
?>
以上内容希望帮助到大家,很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要
或 者关注咱们下面的知乎专栏 |
|