2010-08-24 33 views
0

我有一個分頁函數,用於我的數據庫搜索,將每頁的結果限制爲25個。但是,我有大約2300個條目,並且當有人執行查詢很多結果我最終在頁面底部有90個左右的分頁鏈接。我想限制分頁導航器一次只顯示10個頁面,並相應地調整頁面瀏覽量。如何將分頁功能導航限制爲每頁10個

我不太清楚如何調整腳本。

任何幫助將不勝感激。

我現在的功能是如此:

$ search_function是獲得正確的URL,$ classical_guitar指的是圖像的javascript函數。

function generate_page_links($cur_page, $num_pages) { 
    global $search_function, $classical_guitarL, $classical_guitarR; 
    $page_links = ''; 

    // If this page is not the first page, generate the "previous" link 
    if ($cur_page > 1) { 
    $page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page - 1) . "');\">" . $classical_guitarL . "</a> "; 
    } 
    else { 
    $page_links .= ''; 
    } 

    // Loop through the pages generating the page number links 
    for ($i = 1; $i <= $num_pages; $i++) { 
    if ($cur_page == $i) { 
     $page_links .= ' ' . $i; 
    } 
    else { 
     $page_links .= '<a href="javascript:' . $search_function . "('', '" . $i . "');\"> " . $i . "</a> "; 
    } 

    } 

    // If this page is not the last page, generate the "next" link 
    if ($cur_page < $num_pages) { 
    $page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page + 1) . "');\">" . $classical_guitarR . "</a> "; 
    } 
    else { 
    $page_links .= ''; 
    } 

    return $page_links; 
} 

回答

1

在這裏,我已經修改了你的函數:

<?php 

function generate_page_links($cur_page, $num_pages) 
{ 
    global $search_function, $classical_guitarL, $classical_guitarR; 
    $page_links = ''; 


    // If this page is not the first page, generate the "previous" link 
    if ($cur_page > 1) 
    { 
     $page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page - 1) . "');\">" . $classical_guitarL . "</a> "; 
    } 
    else 
    { 
     $page_links .= ''; 
    } 

    $pager_num = 7; // How many page number you wish to display to the left and right sides of the current page 
    $index_start = ($cur_page - $pager_num) <= 0 ? 1 : $cur_page - $pager_num; 
    $index_finish = ($cur_page + $pager_num) >= $num_pages ? $num_pages : $cur_page + $pager_num; 
    if (($cur_page - $pager_num) > 1) { $page_links .= '...'; } // Display ... when there are more page items than $page_num 

    // Loop through the pages generating the page number links 
    // NOTE: I've modified the for index pointers here... 
    for ($i = $index_start; $i <= $index_finish; $i++) 
    { 
     if ($cur_page == $i) 
     { 
      $page_links .= ' ' . $i; 
     } 
     else 
     { 
      $page_links .= '<a href="javascript:' . $search_function . "('', '" . $i . "');\"> " . $i . "</a> "; 
     } 
    } 

    if (($cur_page + $pager_num) < $num_pages) { $page_links .= '...'; } // Display ... when there are more page items than $page_num 

    // If this page is not the last page, generate the "next" link 
    if ($cur_page < $num_pages) 
    { 
     $page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page + 1) . "');\">" . $classical_guitarR . "</a> "; 
    } 
    else 
    { 
     $page_links .= ''; 
    } 

    return $page_links; 
} 

?> 

希望這是有幫助的...

+0

太謝謝你了!這工作完美。 – MBguitarburst 2010-08-24 08:17:53