0

我正在我的网站中显示来自数据库的搜索结果。我能够显示页数的链接,并在第一页上显示前几个结果。但是当我转到第 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 = ($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> "; 
};      
4

1 回答 1

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 查询,或者您可以将其存储在会话、文件或数据库中。无论如何,在您的情况下,一个快速的解决方案是在 set search parameters 部分之前添加以下代码,它将 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;
}
于 2013-08-24T21:32:14.007 回答