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 = $pagetotal) $maxpg = $pagetotal + 1; }elseif($page > ceil(($showcount)/2) && $page = $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 . $i .; }else{ $linkpage .= $i; } } if($page 下一页; $linkpage .= 最后一页; } } if($listrows page = $linkpage; if(1 == $pagetotal) $this->page = ''; return $array; } } 前台html代码:
?