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> ";
};
感謝有這麼隨着我所做的更改,我總共獲得了3頁,但在第二頁上仍然沒有結果。是否因爲從第二頁移動$ sqlPage中的變量不會轉移過來? – patgarci
由於您仍然有3頁,應該有第二和第三的內容。我猜測(不能從你的帖子推斷出來)是你查詢中使用的一個或多個變量不再保留一個值。你可以在第二頁上回顯用於查詢'$ sql'的SQL並將其發佈到此處嗎? – vollie
不知道如何回顯SQL,但我確實添加了我遺漏的代碼部分,這是執行sql查詢所需的變量。 – patgarci