Javascript 如何使用ajax对正在显示和筛选的记录进行分页
我有一个页面,列出了页面上的所有属性。此记录根据单击的复选框进行筛选。如何进行分页。如果未应用任何筛选器,我希望对记录进行分页。同时应用筛选时,我希望对显示的记录也应用分页。我该怎么做?下面是一个例子 执行过滤的示例代码。多谢各位 这是我到目前为止的分页代码。但如何将其应用于过滤记录Javascript 如何使用ajax对正在显示和筛选的记录进行分页,javascript,php,ajax,mysqli,Javascript,Php,Ajax,Mysqli,我有一个页面,列出了页面上的所有属性。此记录根据单击的复选框进行筛选。如何进行分页。如果未应用任何筛选器,我希望对记录进行分页。同时应用筛选时,我希望对显示的记录也应用分页。我该怎么做?下面是一个例子 执行过滤的示例代码。多谢各位 这是我到目前为止的分页代码。但如何将其应用于过滤记录 $totalProperty = "SELECT * FROM property_listing WHERE property_status = 'approved' "; $allProperty = mys
$totalProperty = "SELECT * FROM property_listing WHERE property_status = 'approved' ";
$allProperty = mysqli_query($connection, $totalProperty);
$totalProperties = mysqli_num_rows($allProperty);
$showRecordPerPage = 4;
if(isset($_GET['page']) && !empty($_GET['page'])){
$currentPage = $_GET['page'];
}else{
$currentPage = 1;
}
$startFrom = ($currentPage * $showRecordPerPage) -
$showRecordPerPage;
$lastPage = ceil
($totalProperties/$showRecordPerPage);
$firstPage = 1;
$nextPage = $currentPage + 1;
$previousPage = $currentPage - 1;
分页:
<div class="pagination">
<nav aria-label="Page navigation example">
<ul class="pagination">
<?php if($currentPage != $firstPage) { ?>
<li class="page-item">
<a class="page-link" href="?page=<?php echo $firstPage ?>" tabindex="-1" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<?php } ?>
<?php if($currentPage >= 2) { ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $previousPage ?>"><?php echo $previousPage ?></a></li>
<?php } ?>
<li class="page-item active"><a class="page-link" href="?page=<?php echo $currentPage ?>"><?php echo $currentPage ?></a></li>
<?php if($currentPage != $lastPage) { ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $nextPage ?>"><?php echo $nextPage ?></a></li>
<li class="page-item">
<a class="page-link" href="?page=<?php echo $lastPage ?>" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
<?php } ?>
</ul>
</nav>
</div>
<?php
// This part displays records when filtering is applied
if(isset($_POST["action"])){
$query = "SELECT * FROM property_listing WHERE property_status ='approved'";
// This part filters records based on price range (product prices)
if(isset($_POST["minimum_price"], $_POST["maximum_price"]) && !empty($_POST["minimum_price"]) && !empty($_POST["maximum_price"])){
$query .= " AND property_price BETWEEN '".$_POST["minimum_price"]."' AND '".$_POST["maximum_price"]."'";
}
// This part filters records based in product brand
if(isset($_POST["brand"])){
$brand_filter = implode("','", $_POST["brand"]);
$query .= " AND property_type IN('".$brand_filter."')";
}
// This part filters records based in product ram
if(isset($_POST["ram"])){
$ram_filter = implode("','", $_POST["ram"]);
$query .= " AND property_category IN('".$ram_filter."')";
}
}
$select_all_property = mysqli_query($connection, $query);
$totalcount = mysqli_num_rows($select_all_property);
if($totalcount>0){
while($row = mysqli_fetch_assoc($select_all_property)){
$property_id = $row['property_id'];
$property_link_id = $row['property_link_id'];
$property_title = $row['property_title'];
$property_city = $row['property_city'];
$property_bed = $row['property_bed'];
$property_bath = $row['property_bath'];
$property_date = $row['property_date'];
$property_location = $row['property_location'];
$gallery1 = $row['gallery1'];
$property_price = number_format($row['property_price'],2);
$property_category = $row['property_category'];
$property_type = $row['property_type'];
$property_ad_status = $row['property_ad_status'];
$timeago=get_timeago($property_date);
?>
<div class="item">
<div class="row">
<div class="col-lg-5">
<div class="item-image"> <a href="property_single.php?p_id=<?php echo $property_id; ?>&property_cat=<?php echo $property_category; ?>"><img src="images/property_images/<?php echo $gallery1; ?>" class="img-fluid" alt="">
<div class="item-badges">
<?php if($property_ad_status == "featured"){
echo "<div class='item-badge-left'>Sponsored</div>";
} ?>
<div class="item-badge-right">For <?php echo $property_category; ?></div>
</div>
<div class="item-meta">
<div class="item-price">¢<?php echo $property_price; ?>
<small>$777 / sq m</small>
</div>
</div>
</a>
<a href="#" class="save-item"><i class="fa fa-star"></i></a> </div>
</div>
<div class="col-lg-7">
<div class="item-info">
<h3 class="item-title"><a href="property_single.php?p_id=<?php echo $property_id; ?>&property_cat=<?php echo $property_category; ?>"><?php echo $property_title; ?></a></h3>
<div class="item-location"><i class="fa fa-map-marker"></i> <?php echo $property_location; ?>, <?php echo $property_city; ?></div>
<div class="item-details-i"> <span class="bedrooms" data-toggle="tooltip" title="3 Bedrooms"><?php echo $property_bed; ?> <i class="fa fa-bed"></i></span> <span class="bathrooms" data-toggle="tooltip" title="2 Bathrooms"><?php echo $property_bath; ?> <i class="fa fa-bath"></i></span> </div>
<div class="item-details">
<ul>
<li>Sq Ft <span>730-2600</span></li>
<li>Type <span><?php echo $property_type; ?></span></li>
</ul>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="added-on">Listed <?php echo $timeago; ?> </div>
</div>
<div class="col-md-6">
<a href="#" class="added-by">by John Doe</a>
</div>
</div>
</div>
</div>
</div>
<?php }}else{
echo "<h3>No Data Found</h3>";
}
} ?>
?>
-
-
要生成分页,请使用包含两个参数的LIMIT
子句。第一个参数为OFFSET
,第二个参数为应该从数据库返回多少条记录
这是参考资料:小心!您的代码易受SQL注入攻击。有关更多信息,请查看并感谢@Rahul。不过我是个新手,会努力的。谢谢@Rahul。但是如何应用数据库中的过滤结果呢?这可以通过Jquery、AJAX将值发送到PHP中。如果你需要,我将与你分享参考。请分享参考参考参考:谢谢@Rahul。我的代码就是这样做的。我现在只需要为你发送的参考资料分页。我已经尽力了。非常感谢。
$(document).ready(function(){
filter_data();
function filter_data()
{
$('.filter_data').html('<div id="loading" style="" ></div>');
var action = 'fetch_data';
var limit = 4;
var start = 0;
var actions = 'inactive';
var minimum_price = $('#hidden_minimum_price').val();
var maximum_price = $('#hidden_maximum_price').val();
var brand = get_filter('type');
var ram = get_filter('feature');
$.ajax({
url:"fetch_data.php",
method:"POST",
data:{action:action, minimum_price:minimum_price, maximum_price:maximum_price, brand:brand, ram:ram,},
success:function(data){
$('.filter_data').html(data);
}
});
}
function get_filter(class_name)
{
var filter = [];
$('.'+class_name+':checked').each(function(){
filter.push($(this).val());
});
return filter;
}
$('.common_selector').click(function(){
filter_data();
});
$('#price_range').slider({
range:true,
min:100,
max:9000000,
values:[100, 9000000],
step:100,
stop:function(event, ui)
{
$('#price_show').html(ui.values[0] + ' - ' + ui.values[1]);
$('#hidden_minimum_price').val(ui.values[0]);
$('#hidden_maximum_price').val(ui.values[1]);
filter_data();
}
});
});