PHP碎码——分页类
写了两个版本,还是后面好点,只计算关键数据,返回数组,html代码例外拼接
成都创新互联公司专注于企业全网营销推广、网站重做改版、通化网站定制设计、自适应品牌网站建设、H5页面制作、商城网站定制开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为通化等各大城市提供网站开发制作服务。
entry_total = (int)$entry_total; $this->page_size = (int)$page_size; $this->page_count = ceil($entry_total/$page_size); //分页栏的长度取用户提交的分页栏长度与页面总数中的较小值 $this->page_li_count = $page_li_count > $this->page_count ? $this->page_count : $page_li_count; } /** * 设置当前页及active状态 * @param unknown $page 合法参数为:first end 1234 */ public function setPage($page){ if ($page == 'end') { $this->active = 'end'; $this->page = $this->getPagecount(); }elseif ($page>0 && $page<=$this->page_count) { $this->active = $page; $this->page = $page; }else{ //first或传递非法值的会被定到首页去 $this->active = 'first'; $this->page = 1; } } /** * * @param unknown $url //该url末尾必需带有?或者& */ public function setURL($url){ $this->url = $url; } //当前页码 public function getPage(){ return $this->page; } //共计多少页 public function getPagecount(){ return $this->page_count; } //共计多少条记录 public function getEntrycount(){ return $this->entry_total; } /** * 计算当前页的分页栏需要的信息 * @return 返回数组,包含拼接分页关键信息 */ public function create(){ //首页 $first_page= "first"; //计算上页页码,最小为1 $pre_page = ($this->page-1)<=1 ? 1 : $this->page-1; //计算下一页页码,最大为page_count $next_page = ($this->page+1)>=$this->page_count ? $this->page_count : $this->page+1; //尾页 $end_page= "end"; $nums_page = []; //这个判断与初始化变量中的判断重复了,但...安全点 if ($this->page_count >= $this->page_li_count) { //中间li为第几个,偶数取中偏前的一个 $mid_li = $this->page_li_count%2==0 ? $this->page_li_count/2 : (($this->page_li_count-1)/2)+1; //理想情况下,$mid_li后面li应有的个数 $right_li_num = $this->page_li_count - $mid_li; //理想情况下,$mid_li前面li应有的个数 $left_li_num = $mid_li-1; //计算最右边的页码 if ($this->page <= $left_li_num) { //若当前页左边li个数不足以让其居中,则最右端页码等于最右端li的编号 $right_li_page = $this->page_li_count; }else { $right_li_page = ($this->page_count-$this->page) >= $right_li_num ? $this->page+$right_li_num : $this->page_count; } for ($i = 0; $i < $this->page_li_count ; $i++) { //看着可能有点绕,纸上算算就知道了,已知最右边li的页码,li的总数,当前li的编号为$i+1,算当前li对应的页码 $n = $right_li_page-$this->page_li_count+$i+1; $nums_page[] = $n; } } //将这几个拼接起来 $this->result['first'] = $first_page; $this->result['end'] = $end_page; $this->result['pre'] = $pre_page; $this->result['next'] = $next_page; $this->result['nums'] = $nums_page; $this->result['active'] = $this->active; return $this->result; } public function getHTML(){ //待完善........ $url = isset($this->url) ? $this->url : '?'; $first_class = $this->result['active'] == 'first' ? 'active' : ''; $end_class = $this->result['active'] == 'end' ? 'active' : ''; $pre_class = $this->result['active'] == 'pre' ? 'active' : ''; $next_class = $this->result['active'] == 'next' ? 'active' : ''; $first_page_html = <<result['first']}">首页 HTML; $pre_page_html = <<result['pre']}">上一页 HTML; $next_page_html = <<result['next']}">下一页 HTML; $end_page_html = <<result['end']}">尾页 HTML; $lis_page_html = ''; foreach ($this->result['nums'] as $v){ $li_class = $this->result['active'] == $v ? 'active' : ''; $lis_page_html .= <<$v HTML; } $htmlcode = $first_page_html.$pre_page_html.$lis_page_html.$next_page_html.$end_page_html; return $htmlcode; } } /*********** * setPage( isset($_GET['page'])?$_GET['page']:1); $a->create(); ?>fenye
-
getHTML()?>
分享题目:PHP碎码——分页类
文章转载:http://pwwzsj.com/article/pjidjo.html