我正在尝试将分页应用到我的搜索结果中,但即使我在url bar分页工作中手动更改页码也一切正常,但是当我使用分页链接时,我会丢失所有搜索数据。我的代码是善意的建议我的解决方案。谢谢。
function search(){
global $conn;
$per_page = 3;
$page = $_GET['page'];
$start_from = ($page-1) * $per_page;
if(isset($_GET['search'])){
$property_type = $_GET['property_type'];
$society = $_GET['society'];
$min_area = !empty($_GET['min_area'])? $min_area = $_GET['min_area'] : 'NULL';
$max_area = !empty($_GET['max_area'])? $max_area = $_GET['max_area'] : 'NULL';
$min_price = !empty($_GET['min_price'])? $min_price = $_GET['min_price'] : 'NULL';
$max_price = !empty($_GET['max_price'])? $max_price = $_GET['max_price'] : 'NULL';
$sql = "SELECT * FROM properties
WHERE property_cat LIKE '%$property_type%'
OR property_location LIKE '%$society%'
OR property_area BETWEEN $min_area AND $max_area
OR property_price BETWEEN $min_price AND $max_price
ORDER BY property_id DESC
LIMIT $start_from, $per_page";
//echo $sql; die;
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if($count == 0){
redirect('index.php');
}
while($row = mysqli_fetch_assoc($result)){
echo "<li class='col-md-4 col-sm-6 type-rent maximum-height'>";
echo "<div class='property-block'>";
echo "<a href='property-detail.html' class='property-featured-image'>";
echo "<img src='../admin/property_images/".$row['property_image'].".png' alt='feature image'>";
echo "<span class='images-count'><i class='fa fa-picture-o'></i> 2</span>";
echo "<span class='badges'>";
echo $row['is_parking'] == 0?'Rent':'Buy';
echo "</span>";
echo "</a>";
echo "<div class='property-info'>";
echo "<h4><a href='property-detail.html'>".$row['property_title']."</a></h4>";
echo "<span class='location'>".$row['property_location']."</span>";
echo "<div class='price'><strong>PKR</strong><span>".number_format($row['property_price'])."/-</span></div>";
echo "</div>";
echo "<div class='property-amenities clearfix'>";
echo "<span class='area'><strong>".$row['property_area']."</strong>";
echo $row['property_area_type']==1?'Marla':'Kanal';
echo "</span>";
echo "<span class='baths'><strong>".$row['property_baths']."</strong>Baths</span>";
echo "<span class='beds'><strong>".$row['property_beds']."</strong>Beds</span>";
echo "<span class='parking'><strong>";
echo $row['is_parking'] == 0?'No':'Yes';
echo "</strong>Parking</span>";
echo "</div>";
echo "</div>";
echo "</li>";
}
//Now select all from table for Pagination
$sql = "SELECT * FROM properties
WHERE property_cat LIKE '%$property_type%'
OR property_location LIKE '%$society%'
OR property_area BETWEEN $min_area AND $max_area
OR property_price BETWEEN $min_price AND $max_price";
$result = mysqli_query($conn, $sql);
//Count the total records
$total_records = mysqli_num_rows($result);
//echo $total_records;die;
//Using Ceil Function to round off
$total_pages = ceil($total_records/$per_page);
echo "<div class='text-center'>";
echo "<ul class='pagination'>";
// Going to First Page
echo "<li><a href='search-page.php?page=1&property_type=".$property_type."&society=".$society."&min_area=".$min_area."&max_area=".$max_area."&min_price=".$min_price."&max_price=".$max_price."'>First</a></li>";
// Pagination
for($i=1; $i<=$total_pages; $i++){
echo "<li><a href='search-page.php?page=".$i."&property_type=".$property_type."&society=".$society."&min_area=".$min_area."&max_area=".$max_area."&min_price=".$min_price."&max_price=".$max_price."'>".$i."</a></li>";
}
// Going to Last Page
echo "<li><a href='search-page.php?page=$total_pages&property_type=".$property_type."&society=".$society."&min_area=".$min_area."&max_area=".$max_area."&min_price=".$min_price."&max_price=".$max_price."'>Last</a></li>";
echo "</ul>";
echo "</div>";
}
}