2013-08-17 25 views
0

嗯,我有一個工作腳本(見下文),但它似乎很笨重和多餘的;在我的辯護中,我在很多個月前寫了這個代碼,但那不是重點。我很好奇,如果任何人有一個更有效的方式來寫這個代碼,更少的循環和條件,以及代碼中的噪音。更簡化的動態創建頁面鏈接的方式?

代碼中的問題:

private function pageLinks($num, $page = 1, $search = false, $ne = false) { 
    $query = ($search) ? '&query='.$search : null; 
    $by = (is_numeric($ne)) ? '&by='.$ne : null; 
    $links = 'Page(s):<a href="search.php?page=1' . $query . $by . '" class="tableLink">1</a>'; 
    $count = 1; 
    $npp = $this->numPerPage; 
    $buttons = 9; 
    $half = 4; 
    for($i = 1; $i <= $num; $i++) { 
     if(($i%$npp) === 0) { 
      $count++; 
     } 
    } 
    if($count < $buttons) { 
     for($i = 2; $i <= $count; $i++) { 
      $links .= '<a href="search.php?page=' . $i . $query . $by . '" class="tableLink">' . $i . '</a>'; 
     } 
    } elseif($page <= ($half + 2)) { 
     for($i = 2; $i <= $buttons; $i++) { 
      $links .= '<a href="search.php?page=' . $i . $query . $by . '" class="tableLink">' . $i . '</a>'; 
     } 
     $links .= '...<a href="search.php?page=' . $count . $query . $by . '" class="tableLink">' . $count . '</a>'; 
    } elseif($page <= ($count - ($half + 2))) { 
     $links .= '...'; 
     for($i = $half; $i > 0; $i--) { 
      $links .= '<a href="search.php?page=' . ($page - $i) . $query . $by . '" class="tableLink">' . ($page - $i) . '</a>'; 
     } 
     $links .= '<a href="search.php?page=' . ($page - $i) . $query . $by . '" class="tableLink">' . ($page - $i) . '</a>'; 
     for($i = 1; $i <= $half; $i++) { 
      $links .= '<a href="search.php?page=' . ($page + $i) . $query . $by . '" class="tableLink">' . ($page + $i) . '</a>'; 
     } 
     $links .= '...<a href="search.php?page=' . $count . $query . $by . '" class="tableLink">' . $count . '</a>'; 
    } else { 
     $links .= '...'; 
     for($i = $buttons - 1; $i >= 0; $i--) { 
      $links .= '<a href="search.php?page=' . ($count - $i) . $query . $by . '" class="tableLink">' . ($count - $i) . '</a>'; 
     } 
    } 
    return($links); 
} 

的方法被稱爲像這樣:

$links = $this->pageLinks($count, $page, $url, $ne); 

並且所述變量如這樣:

$count =總的客戶端在數據庫(int)
$page =當前頁面要從(int)
$url =用於搜索(String)
$ne姓名或電子郵件=是用於搜索字符串或者通過域名(1)或電子郵件(2)(int)

和輸出是一樣的東西(如鏈接):

頁(S):123456789 ... 33

或者,如果你在中間的是(第20頁):

頁(S):1 ... 161718192021222324 ... 33

現在這不是總是通過搜索功能調用,因此默認值爲$url$ne,但這並不重要。我的問題是有一個更清潔的方式來處理這些鏈接的構建?或者我堅持這個循環的集羣?

+5

你應該問這樣的http://codereview.stackexchange.com/。 –

+0

@JaredFarrish謝謝你的擡頭,我剛剛做到了。 – faino

回答

1

codereview.stackexchange.com的人的幫助下,我發現了我需要的東西。你可以find the answer here for a more in-depth approach,但這裏是更新的代碼,如果任何人遇到這樣是好奇:

private function pageLinks($num, $page = 1, $search = false, $ne = false) { 
    $query = ($search && is_numeric($ne)) ? "&query=" . $search . "&by=" . $ne : null; 
    $links = "Page(s):" . $this->page_link(1, $query); 
    $npp = $this->numPerPage; 
    $half = 4; 
    $count = floor($num/$npp) + 1; 
    $from = $page - $half; 
    if($from <= 2) { 
     $from = 2; 
    } 
    $to = $page + $half; 
    if($to >= $count - 1) { 
     $to = $count - 1; 
    } 
    if($from > 2) { 
     $links .= "..."; 
    } 
    for($i = $from; $i <= $to; $i++) { 
     $links .= $this->page_link($i, $query); 
    } 
    if($i < $count) { 
     $links .= "..."; 
    } 
    $links .= $this->page_link($count, $query); 
    return($links); 
} 
private function page_link($num, $query) { 
    return("<a href=\"search.php?page=" . $num . $query . "\" class=\"table_link\">" . $num . "</a>"); 
}