2009-09-18 94 views
0

我在拼寫下面的代碼時遇到了困難。我認爲它與將GET變量$ find傳遞到下一頁有關。使用GET變量時的分頁

反正,我怎麼會分頁下面的代碼,使下面的表格顯示了每個頁面只有100行?

由於提前,

約翰

<?php 
ob_start(); 
session_start(); 
$find = strip_tags($_GET['find']); 
$illegal = array("'", ".", "/", "\"", ";", "{", "}", "[", "]", "\\", "''", "'''", "''''", "'''''", "\\\\", "\\\\\\", "\\\\\\\\"); 
$find = str_replace($illegal, '', $find); 
$find = trim ($find); 
$find = strtolower($find); 
$find = stripslashes($find); 
$_SESSION['find'] = $find; 
?> 

<? 
if ($searching =="yes") 
{ 

if ($find == "") 
{ 
session_write_close(); 
header("Location:http://www.site.com/index.php"); 
exit; 
unset($_SESSION['find']); 

} 

mysql_connect("mysqlv10", "username", "password") or die(mysql_error()); 
mysql_select_db("database") or die(mysql_error()); 
$find = mysql_real_escape_string($find); 

$result=mysql_query("SHOW TABLES FROM database LIKE '$find'") 
or die(mysql_error()); 

if(mysql_num_rows($result)>0){ 
while($table=mysql_fetch_row($result)){ 
print "<p class=\"topic\">$table[0]</p>\n"; 
$r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` WHERE site != '' ORDER BY effective_vote DESC"); 

print "<table class=\"navbar\">\n"; 
while($row=mysql_fetch_array($r)){ 

$effective_vote = $row['votes_up'] - $row['votes_down']; 

print "<tr>"; 

print "<td class='sitename'>".'<a type="amzn" category="products" class="links2">'.$row['site'].'</a>'."</td>"; 

print "<td class='votes'>".'<span class="votes_count" id="votes_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>"; 
print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.vote.'</a>'.'</span>'."</td>"; 
} 
print "</tr>\n"; 
} 
print "</table>\n"; 

} 
?> 

回答

0

您需要在SQL語句中使用

LIMIT (<pagenumber*amount of records you want to display>,< amount of records you want to display >) 

2

你必須使用LIMIT在查詢告訴數據庫你想要的行數以及從哪裏開始。

傳遞下去,告訴你想要的結果的另一塊,而不只是第一批腳本的參數。

所以對於鏈接,您可以沿着page參數傳遞:

example.com/results.php?page=2 

page=會告訴你想回到哪個頁面的腳本。

然後你會想LIMIT的行數,每次讓你有一致的分頁返回。

$results_cnt = 100; //--rows you want per page of results 

現在在你的腳本,你會檢查,看看是否page變量已設置。如果不是,則默認從第一個返回的起始行。但是,當你想返回不同的頁面/結果集時,需要一點數學才能開始正確的行。現在

if(isset($_GET["page"]) //--see if the variable is even there 
{ 
    $page_num = (int)$_GET["page"]; //--forcing it to always be an integer 

    $start_row = $results_cnt * ($page_num - 1); 

    /* -- 
    what happens: 
     ($results_cnt currently at 100) 

    on page one (page=1), start at row 0 
     math: 100 * (1 - 1) = 0 

    on page two (page=2), start at row 100 
     math: 100 * (2 - 1) = 100 

    on page three (page=3), start at row 200 
     math: 100 * (3 - 1) = 200 

    etc. 
    */ 
} 
else 
    $start_row = 0; 

,將其具有正確的起始行,調整SQL查詢中使用的變量,像這樣:

$r = mysql_query("SELECT *, votes_up - votes_down AS effective_vote 
        FROM `$table[0]` 
        WHERE site != '' 
        ORDER BY effective_vote DESC 
        LIMIT $start_row, $results_cnt"); 

每次你打它會檢查,看看網頁時,如果$_GET["page"]有。如果不是,則從第一行顯示。如果是,則進行數學計算並確定有多少行要通過並顯示下一頁。