如何使用ajax对记录进行分页显示和过滤

时间:2019-05-28 15:07:04

标签: javascript php ajax mysqli

我有一个页面,列出该页面上的所有属性。该记录是根据单击的复选框进行过滤的。我该如何进行分页。当未应用任何过滤器时,我希望对记录进行分页。当还应用过滤时,我希望分页也应用于显示的记录。我该怎么办?下面是一个 进行过滤的示例代码。谢谢。

page look like this

这是我到目前为止的分页代码。但是,如何将其应用于过滤的记录?

$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">&laquo;</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">&raquo;</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>";

        }

} ?>              
 ?>   

下面也是用于过滤的ajax代码

$(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();
    }
});

});

1 个答案:

答案 0 :(得分:0)

要生成分页,请使用LIMIT子句,该子句将带有两个参数。第一个参数为OFFSET,第二个参数应从数据库返回多少记录。

以下是参考:www.tutorialspoint.com