当前位置:Gxlcms > PHP教程 > thinkphp中对数组元素进展分页显示

thinkphp中对数组元素进展分页显示

时间:2021-07-01 10:21:17 帮助过:4人阅读

thinkphp中对数组元素进行分页显示
 

PHP模块代码:

class NewsAction extends Action{

public $page = '';  //分页,当前页数

 function News(){
  $list = M("News")->field('id,title')->where("status = 5")->findAll();

  //分页 
  $list = $this->array_page($list);

  //结束

  $num = 0;

  $URL = "http://www.baidu.com/";

  $suffixName = '.html';
  foreach($list as $key=>$val){
   $num = $key + 1;
   $str .= '  '.$num.").".$val['title']."
"; } $str = preg_replace('/(.*),\s$/','\\1',$str); $str .= "".$this->page.""; return $str; } //数组分页方法 /** * array $array 数组 * integer $showCount 每页的记录数 *------------------------------ * return array 返回数组 */ function array_page($array,$listRows = 12,$showCount = 4){ $p = C('VAR_PAGE'); $url = $_SERVER['REQUEST_URI']."/".$p."/"; $page = !empty($_GET[C('VAR_PAGE')])?$_GET[C('VAR_PAGE')]:1; $upRow = $page-1; $downRow = $page+1; $linkPage = ""; $totalPage = (int)count($array); $pagetotal = ceil($totalPage/$listRows); //总页数 if($page <= ceil(($showCount)/2)){ $minpg = 1; $maxpg = $showCount+1; if($maxpg >= $pagetotal) $maxpg = $pagetotal + 1; }elseif($page > ceil(($showCount)/2) && $page < ($pagetotal - ceil($showCount/2))){ $minpg = $page - ceil(($showCount)/2) + 1; $maxpg = $page + ceil(($showCount)/2) + 1; if($maxpg >= $pagetotal) $maxpg = $pagetotal; }elseif($page >=$pagetotal - ceil(($showCount)/2)){ $minpg = $pagetotal - ($showCount-1); if($minpg == 1) $minpg = 2; $maxpg = $pagetotal + 1; } $linkPage .= " ".$totalPage." 条记录 ".$page."/".$pagetotal." 页 "; if($pagetotal == 1){ $linkPage .= " 1 "; }else{ if($page > 1){ $linkPage .= " 第一页"; $linkPage .= " 上一页"; } for($i = $minpg; $i < $maxpg; $i++){ if($page == $i){ $linkPage .= " ". $i .""; }else{ $linkPage .= " $i"; } } if($page < $pagetotal){ $linkPage .= " 下一页"; $linkPage .= " 最后一页"; } } if($listRows < $totalPage) { if($page <= 1 || empty($page)) { $page = 0; } else { $page = intval($page) - 1; } $array = array_slice($array,$page*$listRows,$listRows,true); } $this->page = $linkPage; if(1 == $pagetotal) $this->page = ''; return $array; } } 前台Html代码:

?

人气教程排行