2013-08-24 90 views
0

我在我的網站上顯示來自數據庫的搜索結果。我可以顯示指向頁數的鏈接,並在首頁上顯示前幾項結果。但是當我到第2頁時,沒有結果與顯示的第3頁和第4頁相同。PHP分頁數據庫信息

PHP代碼

//This checks to see if there is a page number. If not, it will set it to page 1 
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 5; 

///////////set search variables 
$property = $_POST['property']; 
$bedroom = $_POST['BedroomNumber']; 
$bathroom = $_POST['BathroomNumber']; 
$priceMin = $_POST['PriceMin']; 
$priceMax = $_POST['PriceMax']; 
$sizeMin = $_POST['SizeMin']; 
$sizeMax = $_POST['SizeMax']; 
$termlease = $_POST['TermLease']; 
//////////search 
if(isset($_POST['utilities']) && is_array($_POST['utilities'])) { 
    foreach($_POST['utilities'] as $check) { 
      //echoes the value set in the HTML form for each checked checkbox. 
         //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5. 
         //in your case, it would echo whatever $row['Report ID'] is equivalent to. 
    } 
} 

$sql = $mysqli->query("select * from propertyinfo where Property like '%$property%' and NumBed >= '%$bedroom%' and NumBath >= '%$bathroom%' and Footage >='$sizeMin' and Footage <='$sizeMax' and Price >= '$priceMin' and Price <= '$priceMax' and utilities like '%$check%' and TermLease like '%$termlease%' ORDER BY Price ASC LIMIT $start_from, 5"); 

if($sql->num_rows){ 
    while ($row = $sql->fetch_array(MYSQLI_ASSOC)){ 
     echo '<div id="listing"> 
        <div id="propertyImage"> 
         <img src="uploadimages/'.$row['imageName1'].'" width="200" height="150" alt=""/> 
        </div> 

        <div id="basicInfo"> 
        <h2>$'.$row['Price'].' | '.$row['Footage'].' sqft.</h2> 
        <p style="font-size: 18px;"># '.$row['StreetAddress'].', '.$row['City'].', BC</p> 
        <p>'.$row['NumBed'].' Bedrooms | '.$row['NumBath'].' Bathrooms | '.$row['Property'].'</p> 
        <br> 
        <p><a href="output2.php?record_id='.$row['ID'].'" class="link2" target="_blank">View Full Details</a> 

        </div> 
       </div>'; 



    } 
} 
else 
{ 
echo '<h2>0 Search Results</h2>'; 
} 

$sqlPage = $mysqli->query("select count(id) from propertyinfo where Property like '%$property%' and NumBed >= '%$bedroom%' and NumBath >= '%$bathroom%' and Footage >='$sizeMin' and Footage <='$sizeMax' and Price >= '$priceMin' and Price <= '$priceMax' and utilities like '%$check%' and TermLease like '%$termlease%'"); 
$row2 = $sqlPage->fetch_array(); 
$total_records = $row2[0]; 
$total_pages = $total_records > 0 ? ceil($total_records/5) : 0; 

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

回答

0

出乎你的概述的選擇,你已經有了用於頁面數的選擇沒有過濾器。因此後者將(可能)更高,從而表明不存在的頁面。您應該將$sqlPage一行更改爲以下內容以獲得一定數量的頁面。

$sqlPage = $mysqli->query("select count(id) from propertyinfo where Property like '%$property%' and NumBed >= '%$bedroom%' and NumBath >= '%$bathroom%' and Footage >='$sizeMin' and Footage <='$sizeMax' and Price >= '$priceMin' and Price <= '$priceMax' and utilities like '%$check%' and TermLease like '%$termlease%'"); 

你也應該$total_pages行更改爲以下,因爲你可能另有0誤差得到了分工:

$total_pages = $total_records > 0 ? ceil($total_records/5) : 0; 

編輯IRT意見如下

如果你去下一頁數據不會被轉貼,因此你參數是空的。有幾種方法可確保數據在下一頁可用。您可以輸出表單並重新發布,您可以將數據追加到GET查詢中,也可以將其存儲在會話,文件或數據庫中。不管怎樣,在你的情況下,快速的解決辦法是添加以下代碼只是設置的搜索參數部分,從而節省了POST DAAT的會議前,然後使用該數據,直到新的數據發佈:

if(!isset($_SESSION)) { 
    if([email protected]_start()) die('Could not start session'); 
} 
if(empty($_POST)) { 
    if(isset($_SESSION['searchpost'])) $_POST = $_SESSION['searchpost']; 
} else { 
    $_SESSION['searchpost'] = $_POST; 
} 
+0

感謝有這麼隨着我所做的更改,我總共獲得了3頁,但在第二頁上仍然沒有結果。是否因爲從第二頁移動$ sqlPage中的變量不會轉移過來? – patgarci

+0

由於您仍然有3頁,應該有第二和第三的內容。我猜測(不能從你的帖子推斷出來)是你查詢中使用的一個或多個變量不再保留一個值。你可以在第二頁上回顯用於查詢'$ sql'的SQL並將其發佈到此處嗎? – vollie

+0

不知道如何回顯SQL,但我確實添加了我遺漏的代碼部分,這是執行sql查詢所需的變量。 – patgarci