2012-07-12 115 views
0

我有一個簡單的導航php腳本,它工作正常。 唯一的問題是我需要找到一種方法來在頁面頂部而不是底部顯示分頁鏈接。 因此,對於我來說一個newb,似乎不可能,因爲我必須在實際導航變量被聲明之前顯示分頁鏈接。頁面頂部的分頁鏈接,在實際腳本之前

可以這樣做嗎?

下面是腳本:

<? 
$numrows = '600'; 
$rowsperpage = 20; 
$totalpages = ceil($numrows/$rowsperpage); 
if ($currentpage > $totalpages){ $currentpage = $totalpages; } 
if ($currentpage < 1){ $currentpage = 1; } 
$offset = ($currentpage - 1) * $rowsperpage; 

HERE I query the data from the table and basically fill in the page. 

Now starts the pagination links: 

$range = 3; 
if ($currentpage > 1) { 
    echo '<a href="'.$site_current.'/'.$slug.'/">First</a>'; 
} 
$prevpage = $currentpage - 1; 

echo '<a href="'.$site_current.'/'.$slug.'/'.$prevpage.'/">Previous</a>'; 

for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { 
    if (($x > 0) && ($x <= $totalpages)) {  
     if($x == $currentpage){ 
      echo '<span class="current">'.$x.'</span>'; 
     } else { 

      echo '<a href="'.$site_current.'/'.$slug.'/'.$x.'/">'.$x.'</a>'; 

     } 
    } 
} 

if ($currentpage != $totalpages){ 
    $nextpage = $currentpage + 1;  
    echo '<a href="'.$site_current.'/'.$slug.'/'.$nextpage.'/">Next</a>'; 
    echo '<a href="'.$site_current.'/'.$slug.'/'.$totalpages.'/">Last</a>'; 
} 
?> 
+2

將您顯示的內容與您的邏輯分開。在迴應任何東西之前,計算出你想在變量中顯示什麼。然後,當您獲得所有需要的信息時,請按照最有意義的順序迴應結果。 – MyStream 2012-07-12 21:53:43

+1

爲什麼你引用'$ numrows'?這是一個數字。你將它作爲一個數字來使用。引號只是讓PHP在每次使用它時轉換爲整數。 – 2012-07-12 21:56:28

+0

而不是600有一個變量。 Ty for the coments,只是想通了:)我可以把分頁鏈接放在偏移量下面,然後開始實際的mysql查詢 – webmasters 2012-07-12 21:58:06

回答

0

當我在PHP中做任何事情我做了以下

<?php 
    //Calculate anything I need for the page to be displayed 

    //Build the html page and fill in variables where needed 
?> 

您可能還需要尋找到像智者的邏輯從模板中分離框架設計。 PHP本身就是一種模板語言,但其實我很喜歡使用smarty。

+0

Ty,你是對的,它可以工作......我已經把分頁鏈接放在$ offset =($ currentpage - 1)* $ rowsperpage;然後做我的mysql行foreach顯示分頁數據的循環 – webmasters 2012-07-12 21:59:22

相關問題