2014-10-02 54 views
0

我有一個照片庫的PHP腳本...與一個實際的腳本,我有一個問題,因爲我測試了它,我發現,如果有大量頁面在分頁部分腳本有問題時,顯示的頁數超過25頁...我想截斷顯示的頁面只保留第一個當前頁面(例如,如果總頁數超過10,保持當前10頁或如果總頁數小於10,則保持較少的當前頁面)並保持前導鏈接和下一鏈接完好無損...謝謝!打印分頁截斷大量的頁面在php

實施例:

如果總頁數> 10

< 1 2 3 4 5 6 7 8 9 10>

如果總頁數> 100和我正在瀏覽頁面12

< 3 4 5 6 7 8 9 10 11 12>

if total pages> 100 and I'm viewing page 52

< 43 44 45 46 47 48 49 50 51 52>

並且使得...

但如果總頁數小於10顯示正常分頁...

< 1 2 3 4 5 6 7>

所以應:

先前+ 10 +下一

或者如果小於10

分組+總頁數+未來

我的照片庫與上一張/實際/下一個頁面:

http://i.stack.imgur.com/TIeMB.jpg

在這裏你可以看到這個問題:

http://i.stack.imgur.com/svoHf.jpg

這是主腳本的一部分...有沒有一種方法可以截斷頁面超過25件物品?

// display pagination 
    function print_pagination($numPages,$currentPage) { 
    $p = htmlentities($p, ENT_QUOTES); 
    $currentPage = htmlentities($currentPage, ENT_QUOTES); 
    echo 'Pagina '. $currentPage .' di '. $numPages; 
    if ($numPages > 1) { 
     echo '&nbsp;&nbsp;'; 
     if ($currentPage > 1) { 
     $prevPage = $currentPage - 1; 
     echo '<a href="'. $_SERVER['PHP_SELF'] .'?p='. $prevPage.'" class="direction">&#8678;</a>'; 
     } 
     for ($e = 0; $e < $numPages; $e++) { 
     $p = $e + 1; 
     if ($p == $currentPage) { 
      $class = 'current-paginate'; 
     } else { 
      $class = 'paginate'; 
     } 
     echo '<a class="'. $class .'" href="'. $_SERVER['PHP_SELF'] .'?p='. $p .'">'. $p .'</a>'; 
     } 
     if ($currentPage != $numPages) { 
     $nextPage = $currentPage + 1; 
     echo '<a href="'. $_SERVER['PHP_SELF'] .'?p='. $nextPage.'" class="direction">&#8680;</a>'; 
     } 
    } 
    } 

與編輯通過HD建議我獲得:

// display pagination 
    function print_pagination($numPages,$currentPage) { 
    $p = htmlentities($p, ENT_QUOTES); 
    $currentPage = htmlentities($currentPage, ENT_QUOTES); 
    echo 'Pagina '. $currentPage .' di '. $numPages; 
    if ($numPages > 1) { 
     echo '&nbsp;&nbsp;'; 
     if ($currentPage > 1) { 
     $prevPage = $currentPage - 1; 
     echo '<a href="'. $_SERVER['PHP_SELF'] .'?p='. $prevPage.'" class="direction">&#8678;</a>'; 
     } 
     for ($e = 0; $e < $numPages; $e++) { 
     $p = $e + 1; 
     if ($p == $currentPage) { 
      $class = 'current-paginate'; 
     } else { 
      $class = 'paginate'; 
     } 
     if ($currentPage == $p) { 
      echo ' <a class="'. $class .'" href="'. $_SERVER['PHP_SELF'] .'?p='. $p .'">'. $p .'</a> '; 
      break; 
     } 
     } 
     if ($currentPage != $numPages) { 
     $nextPage = $currentPage + 1; 
     echo '<a href="'. $_SERVER['PHP_SELF'] .'?p='. $nextPage.'" class="direction">&#8680;</a>'; 
     } 
    } 
    } 

我的畫廊與截斷頁碼:Pagina 45二59⇦45⇨

其他網頁缺少...

謝謝!

+1

我真的不明白這個問題。如果有100頁,並且我在第1頁上,那麼我應該看1-10頁的頁面按鈕。如果我在第30頁,我應該看看是什麼? – 2014-10-02 15:36:44

+0

http://i.stack.imgur.com/svoHf.jpg看到這個...... – user3584233 2014-10-02 15:42:55

+0

如果有100頁,而且我在第30頁上,應該顯示哪些頁面標籤?當我在第99頁時呢? – 2014-10-02 15:57:25

回答

0

一旦迭代完成,您可以從for循環中簡單地break;

for ($e = 0; $e < $numPages; $e++) { 
    $p = $e + 1; 
    if ($p == $currentPage) { 
     $class = 'current-paginate'; 
    } else { 
     $class = 'paginate'; 
    } 
    echo '<a class="' . $class . '" href="' . $_SERVER['PHP_SELF'] . '?p=' . $p . '">' . $p . '</a>'; 

    if ($currentPage != $numPages) { 
     $nextPage = $currentPage + 1; 
     echo '<a href="' . $_SERVER['PHP_SELF'] . '?p=' . $nextPage . '" class="direction">&#8680;</a>'; 
    } 


    //Come out of the loop after 10 pages... 
    if (($currentPage + 10) == $p) { 
     break; 
    } 
} 
+0

我測試了您的代碼並且不工作.. ⇨45⇨46⇨47⇨48⇨49⇨⇨⇨50⇨51⇨52⇨53⇨54⇨55⇨56⇨57⇨⇨⇨⇨⇨⇨ – user3584233 2014-10-02 16:05:08

+0

試着用'$ p'的值來代替 – 2014-10-02 16:06:35

+0

明天就試試。 .. 謝謝!現在我必須去吃晚飯了...... – user3584233 2014-10-02 17:14:48