field('id,title')->where("stat"/> field('id,title')->where("stat">

Home >Backend Development >PHP Tutorial > thinkphp中对数组元素进展分页显示

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

WBOY
WBOYOriginal
2016-06-13 13:07:50835browse

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="%22.%24URL.%24val%5B" id 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 = $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 .= " <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 <font color='\"#FF0000\"'>". $i ."</font>";
    }else{
     $linkPage .= "  <a href="javascript:void(0)" onclick="\"showpage('".$url.$i."');\"">$i</a>";
    }
   }
   if($page 下一页";
    $linkPage .= "  <a href="javascript:void(0)" onclick="\"showpage('".$url.$pagetotal."');\"">最后一页</a>";
   }
  }   
  
  if($listRows page = $linkPage;
  if(1 == $pagetotal) $this->page = '';
  return $array;  
 } 

}

 

前台Html代码:





<script type="text/javascript" charset="utf-8" src="__PUBLIC__/js/artDialog/artDialog.min.js"></script>





<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+'',
  width:220
 });
 return testDialog;
}

</script>

<div id="con"></div>




?

Statement:
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