寫(xiě)了兩個(gè)版本,還是后面好點(diǎn),只計(jì)算關(guān)鍵數(shù)據(jù),返回?cái)?shù)組,html代碼例外拼接
成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、通化網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁(yè)面制作、商城網(wǎng)站定制開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為通化等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
entry_total = (int)$entry_total; $this->page_size = (int)$page_size; $this->page_count = ceil($entry_total/$page_size); //分頁(yè)欄的長(zhǎng)度取用戶提交的分頁(yè)欄長(zhǎng)度與頁(yè)面總數(shù)中的較小值 $this->page_li_count = $page_li_count > $this->page_count ? $this->page_count : $page_li_count; } /** * 設(shè)置當(dāng)前頁(yè)及active狀態(tài) * @param unknown $page 合法參數(shù)為: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或傳遞非法值的會(huì)被定到首頁(yè)去 $this->active = 'first'; $this->page = 1; } } /** * * @param unknown $url //該url末尾必需帶有?或者& */ public function setURL($url){ $this->url = $url; } //當(dāng)前頁(yè)碼 public function getPage(){ return $this->page; } //共計(jì)多少頁(yè) public function getPagecount(){ return $this->page_count; } //共計(jì)多少條記錄 public function getEntrycount(){ return $this->entry_total; } /** * 計(jì)算當(dāng)前頁(yè)的分頁(yè)欄需要的信息 * @return 返回?cái)?shù)組,包含拼接分頁(yè)關(guān)鍵信息 */ public function create(){ //首頁(yè) $first_page= "first"; //計(jì)算上頁(yè)頁(yè)碼,最小為1 $pre_page = ($this->page-1)<=1 ? 1 : $this->page-1; //計(jì)算下一頁(yè)頁(yè)碼,最大為page_count $next_page = ($this->page+1)>=$this->page_count ? $this->page_count : $this->page+1; //尾頁(yè) $end_page= "end"; $nums_page = []; //這個(gè)判斷與初始化變量中的判斷重復(fù)了,但...安全點(diǎn) if ($this->page_count >= $this->page_li_count) { //中間li為第幾個(gè),偶數(shù)取中偏前的一個(gè) $mid_li = $this->page_li_count%2==0 ? $this->page_li_count/2 : (($this->page_li_count-1)/2)+1; //理想情況下,$mid_li后面li應(yīng)有的個(gè)數(shù) $right_li_num = $this->page_li_count - $mid_li; //理想情況下,$mid_li前面li應(yīng)有的個(gè)數(shù) $left_li_num = $mid_li-1; //計(jì)算最右邊的頁(yè)碼 if ($this->page <= $left_li_num) { //若當(dāng)前頁(yè)左邊li個(gè)數(shù)不足以讓其居中,則最右端頁(yè)碼等于最右端li的編號(hào) $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++) { //看著可能有點(diǎn)繞,紙上算算就知道了,已知最右邊li的頁(yè)碼,li的總數(shù),當(dāng)前l(fā)i的編號(hào)為$i+1,算當(dāng)前l(fā)i對(duì)應(yīng)的頁(yè)碼 $n = $right_li_page-$this->page_li_count+$i+1; $nums_page[] = $n; } } //將這幾個(gè)拼接起來(lái) $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']}">首頁(yè) HTML; $pre_page_html = <<result['pre']}">上一頁(yè) HTML; $next_page_html = <<result['next']}">下一頁(yè) HTML; $end_page_html = <<result['end']}">尾頁(yè) 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