2016-01-26 125 views
1

我目前正在搜索我的數據庫並顯示每頁5個結果,然後我打算向用戶提供指向其他頁面的鏈接。PHP MySQL分頁問題 - 其他頁面顯示所有行

當我運行此代碼時,它將我的初始查詢限制爲5條記錄,但是當我單擊其他頁面的鏈接時,它將忽略我的sql查詢參數並重新調整數據庫中的所有行。

有誰知道爲什麼會發生這種情況?

這裏是我的代碼:

<html> 
<head> 
<link rel="stylesheet" type="text/css" href="results.css"> 
</head> 
<img src="http://meritscholarshiplist.com/wordpress/wp- content/uploads/2015/12/searchhead.png" alt="" width="100%" style="padding-top:0px;"/> 
<a href="http://meritscholarshiplist.com/wordpress/search-2#searcht|0"><-Search Again</a> 
</html> 
<?php 
require('./wp-blog-header.php'); 
$current_user = wp_get_current_user(); 
$school = $_POST['school']; 
$num_rec_per_page=5; 
mysql_connect('', '', ''); 
mysql_select_db(''); 

if (isset($_GET["page"])) { 
    $page = $_GET["page"]; 
} else { 
    $page=1; 
} 

; 
$start_from = ($page-1) * $num_rec_per_page; 
$sql = "SELECT * FROM `data` WHERE `school` LIKE '%$school%' LIMIT $start_from, $num_rec_per_page"; 
$rs_result = mysql_query ($sql); 
//run the query 
while ($array = mysql_fetch_row($rs_result)) { 
    echo "<div class='list' style='width:1750px; margin:0 auto;'>"; 
    echo "<form action='singleview.php' method='post' target='_new$array[0]'><table id='scholarship' style='float:left;' align='center'><thead><th class='head' colspan='3' style='text-align:center;'>$array[2]<br><br>$array[5]<br><br>$array[3]<br><br><input type='hidden' name='id' value='$array[0]'><input type='submit' value='View More Details'><br>(Will Open in a New Tab)</th></thead>"; 
    echo "</form></table></div> "; 
} 

; 
?> 
<?php 
$school = $_POST['school']; 
$sql = "SELECT * FROM `data` WHERE `school` LIKE '%$school%'"; 
$rs_result = mysql_query($sql); 
//run the query 
$total_records = mysql_num_rows($rs_result); 
//count number of records 
$total_pages = ceil($total_records/$num_rec_per_page); 
echo "<a href='search.php?page=1'>".'|<'."</a> "; 
// Goto 1st page 
for ($i=1; $i<=$total_pages; $i++) { 
    echo "<a href='search.php?page=".$i."'>".$i."</a> "; 
} 

; 
echo "<a href='search.php?page=$total_pages'>".'>|'."</a> "; 
// Goto last page 
echo "<br>Your Search Returned $total_records Results"; 
?> 
+0

因爲$ num_rec_per_page是硬編碼的我沒有看到怎麼樣你描述是可能的。 'echo $ sql;'是限制集? –

回答

1

當你點擊頁面數,瀏覽器正在無school的價值GET請求。您可以先添加school請求像

echo "<a href='search.php?page=1&school=".urlencode($school)."'>".'|<'."</a> "; 
// Goto 1st page 
for ($i=1; $i<=$total_pages; $i++) { 
    echo "<a href='search.php?school=".urlencode($school)."&page=".$i."'>".$i."</a> "; 
} 

,並確保你能獲得的school來自GETPOST值:

$school = $_REQUEST['school']; // instead of $_POST['school']