2012-11-17 63 views
-2

我的博客上的頁面功能並沒有減輕它的重量。當第二頁被點擊時,它跳到第三頁。當第一頁被點擊時,第二頁加載。我在網上發現了這個功能;這不是我自己的。頁面亂七八糟

function generate_pages($total,$current) 
{ 
    if($total > 1) 
    { 
     $total=intval($total); 

     $output='<div class="page"><b>Pages:</b> '; 
     $current_page= (false == isset($current)) ? 1 : $current; 
     for($page=1;$page<$total+1;$page++) 
     { 
      $lower=$current_page-3; 
      $upper=$current_page+3; 
      $special = ($page==$current_page) ? " class=\"current\"" : ""; 
      if(($page > $lower && $page < $upper) || $page < 2 || $page > ($total-1)) 
      { 
       if($last_done_page+1 != $page) $output.= '... '; 
       $output.='<b>[<a'.$special.' href="?pg='.$page.'">'.$page.'</a>] </b>'; 
       $last_done_page=$page; 
      } 
     } 
     $output.='</div><br/>'; 
     return $output; 
    } 
} 
+0

是的,這確實是可怕的...... – jtheman

+0

你是如何解決這個問題的,你是否調試過它?你爲什麼不寫一個更好的函數,而不是依賴於互聯網上的隨機垃圾? – markus

+1

該代碼是從這個問題:[我怎樣才能改善這個PHP分頁算法?](http://stackoverflow.com/questions/3744794/how-can-i-improve-this-php-pagination-algorithm) – sachleen

回答

0

我認爲這些變化將幫助您:

function generate_pages($total,$current,$resultsPerPage) 
{ 
    if($total > 1) 
    { 
     $total=intval($total/$resultsPerPage); 

    if($total%$resultsPerPage>0) 
    { 
     $total=$total+1; 
    } 



     $output='<div class="page"><b>Pages:</b> '; 
     $current_page= (false == isset($current)) ? 1 : $current; 
     for($page=1;$page<$total+1;$page++) 
     { 
      $lower=$current_page-3; 
      $upper=$current_page+3; 
      $special = ($page==$current_page) ? " class=\"current\"" : ""; 
      if(($page > $lower && $page < $upper) || $page < 2 || $page > ($total-1)) 
      { 
       if($last_done_page+1 != $page) $output.= '... '; 
       $output.='<b>[<a'.$special.' href="?pg='.$page.'">'.$page.'</a>] </b>'; 
       $last_done_page=$resultsPerPage; 
      } 
     } 
     $output.='</div><br/>'; 
     return $output; 
    } 
} 

我加入了一個額外的參數$ resultsperPage .......

相關問題