Не получается сделать подгрузку товаров при прокрутке по определенным параметрам.
Можно ли как-то передать данные в файл не через while, то есть чтобы не преобразовывать в json code.
Вот скрипт подгрузки(load.php):
require_once 'config.inc.php';
$num = $_POST['num'];
$query = "
SELECT * FROM product_digiseller WHERE in_stock = '1'
";
if (isset($_POST["minimum_price"], $_POST["maximum_price"]) && !empty($_POST["minimum_price"]) && !empty($_POST["maximum_price"])) {
$query .= "
AND price BETWEEN '" . $_POST["minimum_price"] . "' AND '" . $_POST["maximum_price"] . "'
";
}
if (isset($_POST["activation"])) {
$brand_filter = implode("','", $_POST["activation"]);
$query .= "
AND activation IN('" . $brand_filter . "')
";
}
if (isset($_POST["type"])) {
$color_filter = implode("','", $_POST["type"]);
$query .= "
AND type IN('" . $color_filter . "')
";
}
if (isset($_POST["genres"])) {
$gender_filter = implode("','", $_POST["genres"]);
$query .= "
AND genres1 IN('" . $gender_filter . "')
";
}
$query .= "LIMIT {$num}, 10";
$result = mysqli_query($db, $query);
$news = array();
while ($row = mysqli_fetch_array($result)) {
$news[] = $row;
}
echo json_encode($news);
А это java код:
$(document).ready(function(){
var inProcess = false;
var num = 20;
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height() && !inProcess) {
$.ajax({
url: 'load.php',
method: 'POST',
data: {"num" : num},
beforeSend: function() {
inProcess = true;
}
}).done(function(data){
data = jQuery.parseJSON(data);
if (data.length > 0) {
$.each(data, function(index, data){
$(".filter_data").append("<div class='col-md-4 col-sm-6'><div class='product-grid'><div class='product-image4'><a href='#'><img class='pic-1' src='https://graph.digiseller.ru/img.ashx?id_d=" + data.id_digi + "&crop=true'><img class='pic-2' src='https://graph.digiseller.ru/img.ashx?id_d=" + data.id_digi + "&crop=true'></a><ul class='social'><li><a href='#' data-tip='Quick View'><i class='fa fa-eye'></i></a></li><li><a href='#' data-tip='Add to Wishlist'><i class='fa fa-shopping-bag'></i></a></li><li><a href='#' data-tip='Add to Cart'><i class='fa fa-shopping-cart'></i></a></li></ul><span class='product-new-label'>New</span><span class='product-discount-label'>-10%</span></div><div class='product-content'><h3 class='title'><a href='#'>" + data.name + "</a></h3><div class='price'>" + data.price + "<span>" + data.price + "</span></div><p>Тип: " + data.type + " <br />Активация: " + data.activation + " <br />Жанр: " + data.genres1 + " </p><a class='add-to-cart' href=''>ADD TO CART</a></div></div></div>");
});
inProcess = false;
num += 9;
}
});
}
});
});
Это код вывода товаров по параметрам, которые отмечаются checkbox'ами(fetch.php):
<?php
include('conn.php'); //Database Connection
if (isset($_POST["action"])) {
$query = "
SELECT * FROM product_digiseller WHERE in_stock = '1'
";
if (isset($_POST["minimum_price"], $_POST["maximum_price"]) && !empty($_POST["minimum_price"]) && !empty($_POST["maximum_price"])) {
$query .= "
AND price BETWEEN '" . $_POST["minimum_price"] . "' AND '" . $_POST["maximum_price"] . "'
";
}
if (isset($_POST["activation"])) {
$brand_filter = implode("','", $_POST["activation"]);
$query .= "
AND activation IN('" . $brand_filter . "')
";
}
if (isset($_POST["type"])) {
$color_filter = implode("','", $_POST["type"]);
$query .= "
AND type IN('" . $color_filter . "')
";
}
if (isset($_POST["genres"])) {
$gender_filter = implode("','", $_POST["genres"]);
$query .= "
AND genres1 IN('" . $gender_filter . "')
";
}
if (!isset($_POST["activation"]) or !isset($_POST["type"]) or isset($_POST["genres"]) or isset($_POST["type"])) {
$query .= "LIMIT 20";
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$total_row = $statement->rowCount();
$output = '';
if ($total_row > 0) {
foreach ($result as $row) {
$output .= '
<div class="col-md-4 col-sm-6">
<div class="product-grid">
<div class="product-image4">
<a href="#">
<img class="pic-1" src="https://graph.digiseller.ru/img.ashx?id_d='.$row['id_digi'].'&crop=true">
<img class="pic-2" src="https://graph.digiseller.ru/img.ashx?id_d='.$row['id_digi'].'&crop=true">
</a>
<ul class="social">
<li><a href="#" data-tip="Quick View"><i class="fa fa-eye"></i></a></li>
<li><a href="#" data-tip="Add to Wishlist"><i class="fa fa-shopping-bag"></i></a></li>
<li><a href="#" data-tip="Add to Cart"><i class="fa fa-shopping-cart"></i></a></li>
</ul>
<span class="product-new-label">New</span>
<span class="product-discount-label">-10%</span>
</div>
<div class="product-content">
<h3 class="title"><a href="#">' . $row['name'] . '</a></h3>
<div class="price">
$' . $row['price'] . '
<span>$' . $row['price'] . '</span>
</div>
<p>Тип: ' . $row['type'] . ' <br />
Активация: ' . $row['activation'] . ' <br />
Жанр: ' . $row['genres1'] . ' </p>
<a class="add-to-cart" href="">ADD TO CART</a>
</div>
</div>
</div>';
}
} else {
$output = '<h3>Совпадений нет</h3>';
}
echo $output;
}
?>