thinkphp实现分页

2015-12-26 05:13 发布者: admin 阅读:1628



public function alist()
{
$this->validateLogin();
$this->createurl();
$article = M('article');
$page = I('get.page');
$perpage = 20;
$page = (isset($page) && ($page>0)) ? $page : 1;//current page
$last = $page - 1;
$next = $page + 1;
$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . "?page=";
$sum = count($article->where("is_delete=0")->select());
$pages = ceil($sum/$perpage);
$this->assign('sum',$sum);
$this->assign('pages' ,$pages);
if ($page > 1) {
$arr = "<a href='" . $url . "1''>首页</a>";
$arr .= "<a href='" . $url . $last . "''>上一页</a>";
} else {
$arr = "<a href='javascript:void(0)' class='noallow'>首页</a>";
$arr .= "<a href='javascript:void(0)' class='noallow'>上一页</a>";
}

if ( ($page-2)>0 ) {
$arr .= "<a href='" . $url . $last . "'' class=\"number\">" . ($page-2) . "</a>";
$arr .= "<a href='" . $url . $last . "'' class=\"number\">" . ($page-1) . "</a>";
} else if( (($page-2)<=0) && (($page-1)>0) ) {
$arr .= "<a href='" . $url . $last . "'' class=\"number\">" . ($page-1) . "</a>";
}

$arr .= "<a href='" . $url . $last . "'' class=\"number current\">" . ($page) . "</a>";

if ( ($page+1)<=$pages ) {
$arr .= "<a href='" . $url . $next . "'' class=\"number\">" . ($page+1) . "</a>";
if ( ($page+2)<=$pages ) {
$arr .= "<a href='" . $url . $next . "'' class=\"number\">" . ($page+2) . "</a>";
}
}
if ($page < $pages) {
$arr .= "<a href='" . $url . $next . "''>下一页</a>";
$arr .= "<a href='" . $url . $pages . "''>尾页</a>";
} else {
$arr .= "<a href='javascript:void(0)' class='noallow'>下一页</a>";
$arr .= "<a href='javascript:void(0)' class='noallow'>尾页</a>";
}
$this->assign('page',$arr);
$data = $article->where("is_delete=0")->page($page,$perpage)->select();
$this->assign('article',$data);
$this->display();
}

  



来源:红黑联盟


关注公众号:Mcbang_com 了解更多精彩!

关注网络尖刀微信公众号
随时掌握互联网精彩
赞助链接