2013-08-02 21 views
0

您好我正在開發分頁,並希望引用使用函數的鏈接。但是,如果將其作爲函數應用,並且在手動輸入代碼時得到正確的代碼,那麼我只會獲得「上一個」按鈕。如果要我發送整個代碼,請通知我。奮鬥與分頁鏈接從函數調用

function pagelinks(){ 
    global $g,$CurrentPage,$Totalpages,$next,$previous; 
    echo '<div id='.$g.'></div>'; 
    if($CurrentPage != 1){ 
     $previous = $CurrentPage - 1; 
     echo '<a href = "?'.$g.'='.$previous.'">Back</a>'; 
    } 

    if($CurrentPage != $Totalpages) { 
     $next = $CurrentPage + 1; 
     echo '<a class="pagination" href = "?'.$g.'='.$next.'">Next</a> '; 

    } 

    echo '</div>'; 
    $pageLinks = array($previous,$next); 
    return $pageLinks; 
} 

回答

0

我做你的函數的另一個版本:

function pagelinks($g, $currentPage, $totalPages, &$next = null, &$previous = null) 
{ 
    $out = '<div id='.$g.'></div>'; 
    if ($currentPage > 1) { 
     $previous = $currentPage - 1; 
     $out .= '<a href = "?'.$g.'='.$previous.'">Back</a>'; 
    } 
    if ($currentPage < $totalPages) { 
     $next = $currentPage + 1; 
     $out .= '<a class="pagination" href = "?'.$g.'='.$next.'">Next</a> '; 
    } 
    $out .= '</div>'; 
    return $out; 
} 

echo pagelinks('page', 2, 5); 

// if you need $next and $previous 
// just pass variables to function and use after function call 
echo pagelinks('page', 2, 5, $next, $previous); 

echo $next; 
echo $previous; 

我試圖改善一些東西。

  1. 無需全局變量。參數必須傳遞給函數。這確保了每個值都被設置。
  2. 功能中無回聲。鏈接將被返回。
  3. $ previous和$ next可以作爲函數的參考。
  4. 改變的條件:$當前頁> 1和$當前頁< $總頁數