Thursday, 2 January 2020

Unable to load more data when reach bottom in AJAX

Am trying to load more data for SQL when reached bottom but When i try with bellow code no data is loaded from SQL.

So i tried to debug by echo $query and i get

SELECT * FROM allpostdata WHERE sts = '1' AND mca='Vehicle' ORDER BY pdt DESC LIMIT 4 OFFSET 10

i get the right query i also checked for console error but i get nothing, no data is loaded from SQL

can some one help me solving this.

 var flag = 0;
        $(window).scroll(function () {
            if ($(window).scrollTop() >= $(document).height() - $(window).height() - 300)
            { //RUN when the page is almost at the bottom
                flag += 4; //AUTO IN INCREAMENT
                $(document).ready(function () {
                    filter_data();
                    function filter_data() {
                        $.post(
                                "fetch.php",
                                {
                                    action: 'fetch_data',
                                    cate: get_filter('cate'),
                                    brand: get_filter('brand'),
                                    model: get_filter('model'),
                                    sort: get_filter('sort'),
                                    date: get_filter('date'),
                                    offset: flag,
                                    limit: 4
                                }
                        )
                                .done(function (data) {
                                    $('.filter_data').html(data);
                                });
                    }
                    function get_filter(class_name) {
                        var filter = [];
                        $('.' + class_name + ':checked').each(function () {
                            filter.push($(this).val());
                        });
                        return filter;
                    }
                    $('.filter_all').click(function () {
                        filter_data();
                    });
                });
            }
        });

and here is PHP

    if (isset($_POST["action"])) {
    $query = "SELECT * FROM allpostdata WHERE sts = '1' AND mca='Vehicle'";

    if (!empty($_POST['cate'])) {
        $query .= " AND sca IN (" . str_repeat("?,", count($_POST['cate']) - 1) . "?)";
    } else {
        $_POST['cate'] = []; // in case it is not set 
    }

    if (!empty($_POST['brand'])) {
        $query .= " AND product_brand IN (" . str_repeat("?,", count($_POST['brand']) - 1) . "?)";
    } else {
        $_POST['brand'] = []; // in case it is not set 
    }

    if (!empty($_POST['model'])) {
        $query .= " AND mdl IN (" . str_repeat("?,", count($_POST['model']) - 1) . "?)";
    } else {
        $_POST['model'] = []; // in case it is not set 
    }

    if (empty($_POST['sort']) || $_POST['sort'][0] == "date") {
        $query .= " ORDER BY pdt DESC";
    } elseif ($_POST["sort"][0] == "ASC" || $_POST["sort"][0] == "DESC") {
        $query .= " ORDER BY prs " . $_POST['sort'][0];
    }

    if (!empty($_POST['offset']) && isset ($_POST['limit'])) {
        $query .= " LIMIT ".$_POST['limit']." OFFSET ".$_POST['offset']."";
    } else {
        $query .=""; // in case it is not set 
    }
echo $query;
    $stmt = $conn->prepare($query);
    $params = array_merge($_POST['cate'], $_POST['brand'], $_POST['model']);
    $stmt->execute($params);
    $result = $stmt->fetchAll();
    $total_row = $stmt->rowCount();
    $output = '';
    if ($total_row > 0) {
        foreach ($result as $row) {
            $parameter = $row['pid'];
            $hashed = md5($salt . $parameter);
            $output .= '<a href="/single_view.php?p=' . $parameter . '&s=' . $hashed . '" class="w-xl-20 w-lg-20 col-md-3 col-6 p-1 p-lg-2">
                            <div class="card border-0 small">
                                <img class="card-img-top rounded-0" src="/upload/thumb/' . $row["im1"] . '" alt="Card image cap">
                                <div class="card-body pb-0 pt-2 px-0">
                                    <h6 class="card-title text-dark text-truncate">' . ucfirst(strtolower($row['tit'])) . '</h6>
                                    <h6 class="card-subtitle mb-1 text-muted text-truncate small">' . $row['product_brand'] . '&nbsp;/&nbsp;' . $row['mdl'] . '</h6>
                                    <p class="card-text"><strong class="card-text text-dark text-truncate">&#x20B9;&nbsp;' . $row['prs'] . '</strong></p>' . timeAgo($row['pdt']) . '
                                </div>
                            </div>
                        </a>';
        }
    } else {
        $output = '<h3>No Data Found</h3>';
    }
    echo $output;
}

can some one help me how do i load data when scorlled to bottom

before adding load to scroll method

$(document).ready(function () {
            filter_data();
            function filter_data() {
                $.post(
                        "fetch.php",
                        {
                            action: 'fetch_data',
                            cate: get_filter('cate'),
                            brand: get_filter('brand'),
                            model: get_filter('model'),
                            sort: get_filter('sort'),
                            date: get_filter('date')
                        }
                )
                        .done(function (data) {
                            $('.filter_data').html(data);
                        });
            }
            function get_filter(class_name) {
                var filter = [];
                $('.' + class_name + ':checked').each(function () {
                    filter.push($(this).val());
                });
                return filter;
            }
            $('.filter_all').click(function () {
                filter_data();
            });
        });

All i want is when it is scroll to bottom of page it should load next set from DB.



from Unable to load more data when reach bottom in AJAX

No comments:

Post a Comment