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.").<a href='".$URL.$val['id'].$suffixName."' target='_blank'>".$val['title']."</a><br> "; } $str = preg_replace('/(.*),s$/','\1',$str); $str .= "<div class='d_page'>".$this->page."</div>"; 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 .= " <span class='current'>1</span> "; }else{ if($page > 1){ $linkPage .= " <a href='javascript:void(0)' onClick="showpage('".$url."1');">第一页</a>"; $linkPage .= " <a href='javascript:void(0)' onClick="showpage('".$url.$upRow."');">上一页</a>"; } for($i = $minpg; $i < $maxpg; $i++){ if($page == $i){ $linkPage .= " <span class='current'><font color="#FF0000">". $i ."</font></span>"; }else{ $linkPage .= " <a href='javascript:void(0)' onClick="showpage('".$url.$i."');">$i</a>"; } } if($page < $pagetotal){ $linkPage .= " <a href='javascript:void(0)' onClick="showpage('".$url.$downRow."');">下一页</a>"; $linkPage .= " <a href='javascript:void(0)' onClick="showpage('".$url.$pagetotal."');">最后一页</a>"; } } 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代码:<html><head><script type="text/javascript" charset="utf-8" src="__PUBLIC__/js/artDialog/artDialog.min.js"></script></head><body><script language="javascript" type="text/javascript">//分页跳转function showpage(url){ var testDialog; testDialog = dialog('正在加载中...'); $.get(url,null,function(data){ //$("#con").append(data); testDialog.close(); $("#con").html(data); });}//提示缓冲框function dialog(msg){ var testDialog; testDialog = art.dialog({ lock: false, title:'', id:'loaddialog', content:'<font color="#ff0000">'+msg+'</font>', width:220 }); return testDialog;}</script><div id="con"></div></body></html>?
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号