PHP分页数据库信息
我正在我的网站上显示数据库中的搜索结果。我能够有链接的页数显示,并有第一页上的前几个结果。但当我转到第2页时,显示的第3页和第4页没有相同的结果 php代码PHP分页数据库信息,php,database,mysqli,pagination,Php,Database,Mysqli,Pagination,我正在我的网站上显示数据库中的搜索结果。我能够有链接的页数显示,并有第一页上的前几个结果。但当我转到第2页时,显示的第3页和第4页没有相同的结果 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
//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> ";
};
//检查是否有页码。如果没有,它将设置为第1页
if(isset($_GET[“page”]){$page=$_GET[“page”];}else{$page=1;};
$start_from=($page-1)*5;
///////////设置搜索变量
$property=$_POST['property'];
$卧房=$_POST[‘卧房号’];
$BATHORY=$_POST['BathroomNumber'];
$priceMin=$_POST['priceMin'];
$priceMax=$_POST['priceMax'];
$sizeMin=$_POST['sizeMin'];
$sizeMax=$_POST['sizeMax'];
$termlease=$_POST['termlease'];
//////////搜寻
if(isset($\u POST['utilities'])和&is_数组($\u POST['utilities'])){
foreach($_POST['utilities']作为$check){
//回显HTML表单中为每个选中复选框设置的值。
//所以,如果我检查1,3,和5,它会回显值1,值3,值5。
//在您的例子中,它将回显$row['Report ID']等价的任何内容。
}
}
$sql=$mysqli->query("选择*from propertyinfo,其中属性“%$Property%”和NumBed>=“%$DOOMBY%”和NumBath>=“%$BATHORY%”和镜头>=“%$sizeMin”和镜头=“$sizeMin”和镜头=“$priceMin”和镜头=“$priceMin”和镜头=“$priceMin”和镜头=“$priceMin”和镜头=“%$priceMin”和价格=“%$BATHORY%”和NumBath>=“$sizeMin”和镜头>=“$Price”与您的概览选择用于页面计数。因此,后者将(可能)更高,从而指示不存在的页面。您应该将$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%'");
编辑下面的irt评论
如果转到下一页,数据不会被重新发布,因此所有参数都为空。有几种方法可以确保数据在下一页可用。您可以输出表单并重新发布它,可以将数据附加到get查询,也可以将其存储在会话、文件或数据库中。总之,这是一个快速解决方案将在设置搜索参数部分之前添加以下代码,该部分将POST daat保存到会话中,然后使用该数据直到发布新数据:
if(!isset($_SESSION)) {
if(!@session_start()) die('Could not start session');
}
if(empty($_POST)) {
if(isset($_SESSION['searchpost'])) $_POST = $_SESSION['searchpost'];
} else {
$_SESSION['searchpost'] = $_POST;
}
谢谢你的修改,我总共得到了3页,但第二页仍然没有结果。是因为当从第二页开始移动时,$sqlPage中的变量没有传递过来吗?如果你仍然得到3页,那么第二页和第三页应该有可用的内容。我猜是什么(无法从您的帖子中推断)是否查询中使用的一个或多个变量不再具有值。能否在第2页上回显用于查询$SQL
的SQL并将其发布到此处?不确定如何回显SQL,但我添加了我遗漏的代码部分,即执行SQL查询所需的变量。使用其他代码更新了答案,b根据您的编辑,应该可以为您解决此问题。我假设这将在$sql
if(!isset($_SESSION)) {
if(!@session_start()) die('Could not start session');
}
if(empty($_POST)) {
if(isset($_SESSION['searchpost'])) $_POST = $_SESSION['searchpost'];
} else {
$_SESSION['searchpost'] = $_POST;
}