2014-05-01 64 views
1

我正在使用bootstrap分頁以及顯示我的結果的PHP/mysql組合方法(例如results.php?results = 1 for each page)。這工作正常,但實際上顯示頁面底部的分頁div,即哪一個是活動的,無論您是否可以單擊以下或之前的項目(如果實際上有這些頁面上的結果)PHP分頁顯示

所以我已經完成了一個特定的案例,幾乎可以想到任何影響任何事情的組合,但這肯定是無效的。任何人都可以提出一個更好的方法來做到這一點?有些情況會很明顯,比如如果我在第一頁上,我不想在分頁中顯示第0頁和第-1頁,其他頁面不太明顯,就像我在第3頁上一樣,沒有更多的頁面,我想頁面3在中間,因爲有5個數字顯示,但你不應該能夠點擊4和5.

我使用插件的確切副本顯示here除了第一頁/最後一頁之外,我已經添加了左右增加/減少。

<div id="pagination" style="width: 340px; margin-left: auto; margin-right: auto;"> 
<? if ($results == 1) { ?>  
<ul class="pagination"> 
    <li class="disabled"><a href="#">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li> 
    <li class="disabled"><a href="#">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li> 
    <li class="active"><a href="#"><? echo $results; ?> <span class="sr-only">(current)</span></a></li> 
    <li <?if ($num_rows < $num_res) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li> 
    <li <?if ($num_rows < ($num_res * 2)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 3); ?>"><? echo $results + 3; ?></a></li> 
    <li <?if ($num_rows < ($num_res * 4)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 4); ?>"><? echo $results + 4; ?></a></li> 
    <li <?if ($num_rows < ($num_res)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li> 
    <li <?if ($num_rows < ($num_res)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li> 
</ul> 
    <? } ?> 
    <? if ($results == 2) { ?>  
<ul class="pagination"> 
    <li><a href="<? echo $url."&results=".(1); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li> 
    <li><a href="<? echo $url."&results=".($results - 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li> 
    <li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li> 
    <li class="active"><a href="<? echo $url."&results=".($results); ?>"><? echo $results; ?> <span class="sr-only">(current)</span></a></li> 
    <li <?if ($num_rows < ($num_res * 2)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 3); ?>"><? echo $results + 3; ?></a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li> 
</ul> 
    <? } ?> 
    <? if (($results == 3) && (($last_page == 3))) { ?> 
<ul class="pagination"> 
    <li><a href="<? echo $url."&results=".(1); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li> 
    <li><a href="<? echo $url."&results=".($results - 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li> 
    <li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?> </a></li> 
    <li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li> 
    <li class="active"><a href="<? echo $url."&results=".($results); ?>"><? echo $results; ?><span class="sr-only">(current)</span></a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li> 
</ul> 
    <? } ?> 
    <? if (($results > 2) && ($results < ($last_page - 1))) { ?>  
<ul class="pagination"> 
    <li><a href="<? echo $url."&results=".(1); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li> 
    <li><a href="<? echo $url."&results=".($results - 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li> 
    <li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?> </a></li> 
    <li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li> 
    <li class="active"><a href="<? echo $url."&results=".($results); ?>"><? echo $results; ?><span class="sr-only">(current)</span></a></li> 
    <li><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li> 
    <li><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li> 
</ul> 
    <? } ?> 
    <? if (($results > 2) && ($results == ($last_page - 1))) { ?> 
<ul class="pagination"> 
    <li><a href="<? echo $url."&results=".(1); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li> 
    <li><a href="<? echo $url."&results=".($results - 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li> 
    <li><a href="<? echo $url."&results=".($results - 3); ?>"><? echo $results - 3; ?></a></li> 
    <li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?></a></li> 
    <li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li> 
    <li class="active"><a href="<? echo $url."&results=".($results); ?>"><? echo $results ?><span class="sr-only">(current)</span></a></li> 
    <li><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li> 
    <li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li> 
</ul> 
    <? } ?> 
    <? if (($results > 2) && ($results == $last_page) && ($results != 3)){ ?>  
<ul class="pagination"> 
    <li><a href="<? echo $url."&results=".(1); ?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li> 
    <li><a href="<? echo $url."&results=".($results - 1); ?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li> 
    <li><a href="<? echo $url."&results=".($results - 4); ?>"><? echo $results - 4; ?> </a></li> 
    <li><a href="<? echo $url."&results=".($results - 3); ?>"><? echo $results - 3; ?></a></li> 
    <li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?></a></li> 
    <li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li> 
    <li class="active"><a href="<? echo $url."&results=".($results); ?>"><? echo $results ?><span class="sr-only">(current)</span></a></li> 
    <li class="disabled"><a href="#">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li> 
    <li class="disabled"><a href="#">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li> 
</ul> 
    <? } ?> 
</div> 

如果沒有一個感覺與第一部分幫助,我明白了,它的工作原理,因爲它是據我可以告訴。

但我也想知道是否有方法可以在我的class="disabled"上刪除hrefs?或者我只需要在每個人中都加上if語句?謝謝。

編輯:基於@範思哲的建議。我改變了一些東西,我認爲合適,以及一些變量名稱。

$numberOfPages = ceil($num_rows/$num_res); 
$numberOfPages = (int)$numberOfPages; 

$url = explode('&', $_SERVER['REQUEST_URI']); 
function myFilter($string) { 
return strpos($string, 'results=') === false; 
} 

$url = array_filter($url, 'myFilter'); 
$url = implode('&', $url); 

?> 
<ul class="pagination"> 
    <li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php 
if($results !== $numberOfPages){ 
    echo $url.'&results=' . 1; 
} else { echo '#'; } 
?>">&nbsp;<i class="fa fa-lg fa-angle-double-left"></i>&nbsp;</a></li> 
    <li<?php if($results === 1): ?> class="disabled"<?php endif; ?>><a href="<?php 
if($results !== 1){ 
    echo $url.'&results=' . ($results - 1); 
} else { echo '#'; } 

?>">&nbsp;<i class="fa fa-lg fa-angle-left"></i>&nbsp;</a></li> 

<?php 

// Print the pagination... 
// Minimum of 5 pages in the pagination, even if there aren't 5 pages... 
$pageCount = ($numberOfPages < 5) ? 5 : $numberOfPages; 

// Loop through from page 1 until the last page ($pageCount) 
for($i = 1; $i <= $pageCount; $i++) 
{ 
    // Echo out just the beginning of the <li> tag as we don't yet 
    // know if this needs to be disabled... 
    echo '<li'; 
    // Added to show if current page is active 
    if($i === $results) { 
      echo ' class="active"'; 
    } 
    // Check if: 
    // - This current page is greater than the amount of pages of 
    // results we have 
    // - OR, this is the currently selected page 
    if($i > $numberOfPages || $results === $i) 
    { 
      // <li> tag needs the disabled class.... 
      echo ' class="disabled"'; 
    } 
    // Finish the <li> tag and start generating the link 
    echo '><a href="'; 
    // Opposite logic from above... Only if this is a page we know about 
    // AND this is not the current page. 
    if($i < $numberOfPages && $results !== $i) 
    { 
      // The link will be your page, with the get param, and the 
      // current page number we're printing 
      echo $url.'&results=' . $i; 
    } 
    else 
    { 
      // Otherwise just a # (no link) 
      echo '#'; 
    } 
    // Finish this page... Print the page number ($i) 
    echo '">' . $i . '</a></li>'; 
} 
?> 
    <li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php 
if($results !== $numberOfPages){ 
    echo $url.'&results=' . ($results + 1); 
} else { echo '#'; } 
?>">&nbsp;<i class="fa fa-lg fa-angle-right"></i>&nbsp;</a></li> 
<li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php 
if($results !== $numberOfPages){ 
    echo $url.'&results=' . $numberOfPages; 
} else { echo '#'; } 
?>">&nbsp;<i class="fa fa-lg fa-angle-double-right"></i>&nbsp;</a></li> 

<?php 

@Scobey,這工作很好,如預期的,和我原來的帖子一樣,除了一些事情。在我的例子中,我能夠在當前頁面中獲得當前頁面。例如,< < 1 | 2 | | 4 | 5 >>,這就是爲什麼我有+1和-1,+ 2,-2等多種不同的陳述。

其次,我的意圖是在任何時候都顯示5個頁面選項,而不僅僅是最少五個。所以如果我在第6頁,我只會看到< < 4 | 5 | | 7 | 8 >>,不是1-8。可能沒有必要這樣做,這是一件好事。但是,我不想讓分頁中有超過50個不同的頁碼。

+2

任何一個人如何編寫這樣的代碼?或閱讀它。 –

+0

這就是爲什麼我在這裏尋求幫助。就閱讀而言,它只是6個完全相同的UL,結果不同。 – dcclassics

+0

以及你不需要在幾乎相同的塊,功能\循環將死亡這個總共6行 –

回答

3

一般動態分頁工作,只有當你有幾件事

  1. 所有可能的結果(總)的計數
  2. 你想每頁
  3. 當前頁碼顯示結果的數量
  4. 有關應顯示在此頁面上的結果的信息。

試想一下,你有一個名爲Results數據庫表有一大堆你希望你的分頁頁面

1的結果。計算所有可能的結果

這是一個像一個簡單的SQL語句實現的:每頁

SELECT count(*) FROM Results 

2.金額是你能決定自己的價值。

3.當前頁碼

這通常是默認值爲1,但是當分頁發生變化時,它就會通過作爲GET參數傳遞。

4.結果從表

可實現與切片:

SELECT * FROM Results LIMIT 0, 20 

本示例檢索前20個結果中我們的研究結果表...

5。分頁

現在,使用20個結果作爲默認「每頁計數」

$countPerPage = 20; 

// Get the total number of results 
$result = pg_query('SELECT count(*) FROM Results'); // I'm using PostgreSQL for this example 
$totalResultCount = (int)pg_fetch_result($result, 0, 0); 

// The ceil function will round floats up. 
$numberOfPages = ceil($totalResultCount/$countPerPage); 

// Check if we have a page number in the _GET parameters 
if(!empty($_GET) && isset($_GET['page'])) 
{ 
    $page = (int)$_GET['page']; 
} 
else 
{ 
    $page = 1; 
} 

// Check that the page is within our bounds 
if($page < 0) 
{ 
    $page = 1; 
} 
elseif($page > $numberOfPages) 
{ 
    $page = $numberOfPages; 
} 

// Build the query for the results... 
$query = 'SELECT * FROM Results LIMIT ' . ($page - 1) * $countPerPage . ', ' . $countPerPage; 

$results = pg_fetch_all(pg_query($query)); 

// Deal with printing your results etc... 

?> 
<ul class="pagination"> 
    <li<?php if($page === 1): ?> class="disabled"<?php endif; ?>><a href="<?php 
if($page !== 1){ 
    echo 'page.php?page=' . $page - 1; 
} else { echo '#'; } 
?>">&laquo;</a></li> 
<?php 

// Print the pagination... 
// Minimum of 5 pages in the pagination, even if there aren't 5 pages... 
$pageCount = ($numberOfPages < 5) ? 5 : $numberOfPages; 

// Loop through from page 1 until the last page ($pageCount) 
for($i = 1; $i < $pageCount; $i++) 
{ 
    // Echo out just the beginning of the <li> tag as we don't yet 
    // know if this needs to be disabled... 
    echo '<li'; 

    // Check if: 
    // - This current page is greater than the amount of pages of 
    // results we have 
    // - OR, this is the currently selected page 
    if($i > $numberOfPages || $page === $i) 
    { 
      // <li> tag needs the disabled class.... 
      echo ' class="disabled"'; 
    } 
    // Finish the <li> tag and start generating the link 
    echo '><a href="'; 
    // Opposite logic from above... Only if this is a page we know about 
    // AND this is not the current page. 
    if($i < $numberOfPages && $page !== $i) 
    { 
      // The link will be your page, with the get param, and the 
      // current page number we're printing 
      echo 'page.php?page=' . $i; 
    } 
    else 
    { 
      // Otherwise just a # (no link) 
      echo '#'; 
    } 
    // Finish this page... Print the page number ($i) 
    echo '">' . $i . '</a></li>'; 
} 
?> 
<ul class="pagination"> 
    <li<?php if($page === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php 
if($page !== $numberOfPages){ 
    echo 'page.php?page=' . $page + 1; 
} else { echo '#'; } 
?>">&raquo;</a></li> 
<?php 

免責聲明

我只是坐在這裏寫這個,我沒有測試過,但一般的概念是存在的。希望它是有道理的。我沒有時間去測試或改進它,但如果您有興趣,我可能會在稍後修復它。

希望這會有所幫助!

+0

感謝您的全面回覆! 我可能沒有在我的原始文章中明確指出,實際的分頁,顯示和解析結果工作正常。我有一個變量('$ results')來獲取頁碼。我也在我的php '$ num_res = 10; $ offset =($ results - 1)* $ num_res;'這決定了我的MySQL查詢'LIMIT'。 '$ num_res'是一個用戶可選擇的選項(10條結果,25,100) 我會盡我所能轉換您的循環以在我的頁面上工作。 – dcclassics

+0

我在循環中添加了一些註釋,希望能讓它更清楚一點。我只是想建議你放棄原來發布的內容。我不想聽起來很刺耳,但最好的代碼是可擴展的代碼。你想寫點東西,而不是被用戶輸入打破。想象一下,在你的例子中,如果你允許一個普通的Joe添加更多的結果......你每次需要更新頁面時都必須更新你的分頁。 – Scopey

+0

好的,我修改了你的代碼來和我一起工作。如果你有時間,請在我的原始文章中查看我的評論。再次感謝你的回覆。 – dcclassics