Search filter

This commit is contained in:
Jun Barroga 2024-02-20 18:22:42 +08:00
parent 105c93079c
commit 90dbc02101
4 changed files with 205 additions and 128 deletions

View File

@ -1,5 +1,5 @@
<?php
include "../functions.php";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);

View File

@ -51,7 +51,7 @@ function sddProducts()
curl_close($curl);
$json = json_decode($response, true);
$products = array_filter($json, function ($var) {
return ($var['promo'][0]['next-day-delivery'] === "Yes" && $var['product_type'] == '' || $var['promo'][0]['next-day-delivery'] === "Yes" && $var['product_type'] == 'simple' || $var['promo'][0]['next-day-delivery'] === "Yes" && $var['product_type'] == 'variable' );
return ($var['promo'][0]['same-day-delivery'] === "Yes" && $var['product_type'] == '' || $var['promo'][0]['same-day-delivery'] === "Yes" && $var['product_type'] == 'simple' || $var['promo'][0]['same-day-delivery'] === "Yes" && $var['product_type'] == 'variable' );
});
$products = array_values($products);
return $products;

View File

@ -9,11 +9,134 @@ if ($_SESSION["userId"] <> "") {
$_SESSION["isLoggedIn"] = false;
}
$productSearchResult = $_SESSION["productSearchResult"];
// var_dump($product);
// if (isset($_SESSION["productSearchResult"])) {
// $productSearchResult = $_SESSION["productSearchResult"];
// var_dump($productSearchResult)
// Filter products based on selected categories
// $category = strtolower(trim($_GET['category']));
$filteredProducts = [];
// Check if the form is submitted and categories are selected
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$minPrice = ($_POST['minPrice'] !== '' && is_numeric($_POST['minPrice'])) ? floatval($_POST['minPrice']) : null;
$maxPrice = ($_POST['maxPrice'] !== '' && is_numeric($_POST['maxPrice'])) ? floatval($_POST['maxPrice']) : null;
echo "Min Price: " . ($minPrice !== null ? $minPrice : "null") . "<br>";
echo "Max Price: " . ($maxPrice !== null ? $maxPrice : "null") . "<br>";
// Check if categories are selected
if (isset($_POST['category'])) {
$selectedCategories = $_POST['category'];
foreach ($selectedCategories as $selectedCategory) {
$category = strtolower(trim($selectedCategory));
foreach ($productSearchResult['results'] as $result) {
$productCategory = strtolower(trim($result['product']['product_category']));
$productPrice = isset($result['product']['sale_price']) ? $result['product']['sale_price'] : $result['product']['regular_price'];
// Set minPrice to 0 if it's null
if ($minPrice === null) {
$minPrice = 0;
}
// Set maxPrice to a very high value if it's null
if ($maxPrice === null) {
$maxPrice = PHP_FLOAT_MAX;
}
// Check if the product matches the selected category and falls within the price range
if (
$productCategory == $category &&
($productPrice >= $minPrice) &&
($productPrice <= $maxPrice)
) {
$filteredProducts[] = $result;
}
}
}
} else {
// If no category is selected, filter only by price range
foreach ($productSearchResult['results'] as $result) {
$productPrice = isset($result['product']['sale_price']) ? $result['product']['sale_price'] : $result['product']['regular_price'];
// Set minPrice to 0 if it's null
if ($minPrice === null) {
$minPrice = 0;
}
// Set maxPrice to a very high value if it's null
if ($maxPrice === null) {
$maxPrice = PHP_FLOAT_MAX;
}
if (($productPrice >= $minPrice) &&
($productPrice <= $maxPrice)
) {
$filteredProducts[] = $result;
}
}
}
} // If the request method is GET and filter parameters are present in the URL
// If the request method is GET and filter parameters are present in the URL
if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['category'])) {
// Initialize the filtered products array
$filteredProducts = [];
// Get minPrice and maxPrice from the URL
$minPrice = isset($_GET['minPrice']) ? floatval($_GET['minPrice']) : null;
$maxPrice = isset($_GET['maxPrice']) ? floatval($_GET['maxPrice']) : null;
// Set default values for minPrice and maxPrice if they are null
if ($minPrice === null) {
$minPrice = 0;
}
if ($maxPrice === null) {
$maxPrice = PHP_FLOAT_MAX;
}
// Get selected categories
$selectedCategories = isset($_GET['category']) ? $_GET['category'] : [];
// Check if categories are selected
if (!empty($selectedCategories)) {
// Loop through each selected category
foreach ($selectedCategories as $selectedCategory) {
$category = strtolower(trim($selectedCategory));
// Loop through each product in the search result
foreach ($productSearchResult['results'] as $result) {
$product = $result['product'];
$productCategory = strtolower(trim($product['product_category']));
$productPrice = isset($product['sale_price']) ? $product['sale_price'] : $product['regular_price'];
// Check if the product matches the selected category and falls within the price range
if (
$productCategory == $category &&
($productPrice >= $minPrice) &&
($productPrice <= $maxPrice)
) {
$filteredProducts[] = $result;
}
}
}
} else {
// If no category is selected, filter only by price range
foreach ($productSearchResult['results'] as $result) {
$product = $result['product'];
$productPrice = isset($product['sale_price']) ? $product['sale_price'] : $product['regular_price'];
// Check if the product falls within the price range
if (($productPrice >= $minPrice) &&
($productPrice <= $maxPrice)
) {
$filteredProducts[] = $result;
}
}
}
} else {
// If no filter parameters are present, display all products
$filteredProducts = $productSearchResult['results'];
}
?>
<!DOCTYPE html>
@ -164,34 +287,17 @@ $productSearchResult = $_SESSION["productSearchResult"];
<div class="shop-pro-inner list-view">
<div class="row">
<?php
// echo count($productSearchResult["results"]);
// var_dump($productSearchResult["results"][10]["product"]);
// var_dump($_SESSION["productSearchResult"]);
// for($x=0;$x<=11;$x++){
// $pid = rand(0,count($simpleProducts)-1);
$productsPerPage = 10; // Set the number of products to display per page
$currentPage = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$startIndex = ($currentPage - 1) * $productsPerPage;
$endIndex = min($startIndex + $productsPerPage - 1, count($productSearchResult['results']) - 1);
$endIndex = min($startIndex + $productsPerPage - 1, count($filteredProducts) - 1);
for ($x = $startIndex; $x <= $endIndex; $x++) {
$product = $productSearchResult['results'][$x]['product'];
$product = $filteredProducts[$x]['product'];
$vendorOfProduct = getVendorbyId($product['vendor_api_id']);
// $pid = rand(0, count($productSearchResult["results"]) - 1);
// $product = $productSearchResult["results"][$pid]["product"];
$product_image = !empty($product["product_image"]) ? $product["product_image"] : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/No-Image-Placeholder.svg/330px-No-Image-Placeholder.svg.png";
$sale_price = isset($product['sale_price']) ? $product['sale_price'] : null;
$regular_price = isset($product['regular_price']) ? $product['regular_price'] : null;
//var_dump( $productSearchResult["results"][0]["product"]["product_name"]);
// var_dump($productSearchResult);
// if (isset($_SESSION["noProductsFound"]) && $_SESSION["noProductsFound"]) {
// echo '<div class="no-products-found">No products found</div>';
// } else {
// foreach ($productSearchResult["results"] as $productItem) {
// $product = $productItem["product"];
// $product_image = !empty($product["product_image"]) ? $product["product_image"] : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/No-Image-Placeholder.svg/330px-No-Image-Placeholder.svg.png";
?>
<div class="col-lg-4 col-md-6 col-sm-6 col-xs-6 mb-6 pro-gl-content width-100">
<div class="ec-product-inner">
@ -208,66 +314,36 @@ $productSearchResult = $_SESSION["productSearchResult"];
</div>
</div>
<div class="ec-pro-content">
<!-- <h5 class="ec-pro-title"><a href="shop-left-sidebar-col-4.php?id=<?php echo $product["_id"]; ?>"> -->
<h5 class="ec-pro-title"><a href="product-left-sidebar.php?id=<?php echo $product["_id"]; ?>">
<?php echo $product["product_name"] ?></a></h5>
<!-- <div class="ec-pro-rating">
<i class="ecicon eci-star fill"></i>
<i class="ecicon eci-star fill"></i>
<i class="ecicon eci-star fill"></i>
<i class="ecicon eci-star fill"></i>
<i class="ecicon eci-star"></i>
</div> -->
<h5 class="ec-pro-title"><a href="product-left-sidebar.php?id=<?php echo $product["_id"]; ?>"><?php echo $product["product_name"] ?></a></h5>
<div class="ec-pro-list-desc"><?php echo $product["product_description"] ?></div>
<span class="ec-price">
<?php if (isset($product["sale_price"]) && $product["sale_price"] > 0) : ?>
<span class="old-price">&#8369;<?php echo number_format($product["regular_price"], 2, ".", ",") ?></span>
<span class="new-price">&#8369;<?php echo number_format($product["sale_price"], 2, ".", ",") ?></span>
<?php elseif (isset($product["regular_price"]) && $product["regular_price"] != "") : ?>
<span class="new-price">&#8369;<?php echo number_format($product["regular_price"], 2, ".", ",") ?></span>
<?php elseif ($product["regular_price"] == "" || $product["regular_price"] == null) : ?>
<span class="inquire-text">Inquire</span>
<?php else : ?>
<span class="inquire-text">Inquire</span>
<?php endif; ?>
</span>
<!-- <div class="ec-pro-option">
<div class="ec-pro-color">
<span class="ec-pro-opt-label">Color</span>
<ul class="ec-opt-swatch ec-change-img">
<li class="active"><a href="#" class="ec-opt-clr-img" data-src="<?php echo $product["product_image"] ?>" data-src-hover="<?php echo $product["product_image"] ?>" data-tooltip="Gray"><span style="background-color:#e8c2ff;"></span></a></li>
<li><a href="#" class="ec-opt-clr-img" data-src="<?php echo $product["product_image"] ?>" data-src-hover="<?php echo $product["product_image"] ?>" data-tooltip="Orange"><span style="background-color:#9cfdd5;"></span></a></li>
</ul>
</div>
<div class="ec-pro-size">
<span class="ec-pro-opt-label">Size</span>
<ul class="ec-opt-size">
<li class="active"><a href="#" class="ec-opt-sz" data-old="$25.00" data-new="$20.00" data-tooltip="Small">S</a></li>
<li><a href="#" class="ec-opt-sz" data-old="$27.00" data-new="$22.00" data-tooltip="Medium">M</a></li>
<li><a href="#" class="ec-opt-sz" data-old="$30.00" data-new="$25.00" data-tooltip="Large">X</a></li>
<li><a href="#" class="ec-opt-sz" data-old="$35.00" data-new="$30.00" data-tooltip="Extra Large">XL</a></li>
</ul>
</div>
</div> -->
</div>
</div>
</div>
<?php
}
// }
?>
</div>
</div>
<!-- Ec Pagination Start -->
<div class="ec-pro-pagination">
<span>Showing <?php echo $startIndex + 1; ?>-<?php echo $endIndex + 1; ?> of <?php echo count($productSearchResult['results']); ?> item(s)</span>
<span>Showing <?php echo $startIndex + 1; ?>-<?php echo $endIndex + 1; ?> of <?php echo count($filteredProducts); ?> item(s)</span>
<ul class="ec-pro-pagination-inner">
<?php
// Calculate the total number of pages
$totalPages = ceil(count($productSearchResult['results']) / $productsPerPage);
$totalPages = ceil(count($filteredProducts) / $productsPerPage);
// Define the maximum number of pagination links to display
$maxPaginationLinks = 5;
@ -292,8 +368,16 @@ $productSearchResult = $_SESSION["productSearchResult"];
// Display pagination links within the defined range
for ($page = $startPage; $page <= $endPage; $page++) {
$activeClass = ($page == $currentPage) ? 'active' : '';
$filterParams = array('minPrice' => $minPrice, 'maxPrice' => $maxPrice);
if (isset($_POST['category'])) {
$filterParams['category'] = $_POST['category'];
}
// Modify the pagination URL to include the current page number and category filter
$paginationUrl = '?' . http_build_query(array_merge($_GET, array('page' => $page, 'category' => $selectedCategories, 'minPrice' => $minPrice, 'maxPrice' => $maxPrice)));
// Output the pagination link
?>
<li><a class="<?php echo $activeClass; ?>" href="?page=<?php echo $page; ?>"><?php echo $page; ?></a></li>
<li><a class="<?php echo $activeClass; ?>" href="<?php echo $paginationUrl; ?>"><?php echo $page; ?></a></li>
<?php
}
@ -330,77 +414,70 @@ $productSearchResult = $_SESSION["productSearchResult"];
<h3 class="ec-sidebar-title">Category</h3>
</div>
<div class="ec-sb-block-content">
<ul>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" checked /> <a href="#">clothes</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" /> <a href="#">Bags</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" /> <a href="#">Shoes</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" /> <a href="#">cosmetics</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" /> <a href="#">electrics</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" /> <a href="#">phone</a><span class="checked"></span>
</div>
</li>
<li id="ec-more-toggle-content" style="padding: 0; display: none;">
<ul>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" /> <a href="#">Watch</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" /> <a href="#">Cap</a><span class="checked"></span>
</div>
</li>
</ul>
</li>
<li>
<div class="ec-sidebar-block-item ec-more-toggle">
<span class="checked"></span><span id="ec-more-toggle">More
Categories</span>
</div>
</li>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] . '?' . http_build_query(array_merge($_GET, array('page' => ($_SERVER["REQUEST_METHOD"] == "POST" ? 1 : $currentPage), 'category' => $_POST['category'], 'minPrice' => $_POST['minPrice'], 'maxPrice' => $_POST['maxPrice']))); ?>">
<ul>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" name="category[]" value="Electronics" <?php echo (isset($_POST['category']) && in_array('Electronics', $_POST['category'])) ? 'checked' : ''; ?> /> <a>Electronics</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" name="category[]" value="Solar" <?php echo (isset($_POST['category']) && in_array('Solar', $_POST['category'])) ? 'checked' : ''; ?> /> <a>Solar</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" name="category[]" value="E-bike" <?php echo (isset($_POST['category']) && in_array('E-bike', $_POST['category'])) ? 'checked' : ''; ?> /> <a>E-bike</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" name="category[]" value="E-vehicle" <?php echo (isset($_POST['category']) && in_array('E-vehicle', $_POST['category'])) ? 'checked' : ''; ?> /> <a>E-vehicle</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" name="category[]" value="Appliance" <?php echo (isset($_POST['category']) && in_array('Appliance', $_POST['category'])) ? 'checked' : ''; ?> /> <a>Appliance</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" name="category[]" value="Smart Home" <?php echo (isset($_POST['category']) && in_array('Smart Home', $_POST['category'])) ? 'checked' : ''; ?> /> <a>Smart Home</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" name="category[]" value="Home" <?php echo (isset($_POST['category']) && in_array('Home', $_POST['category'])) ? 'checked' : ''; ?> /> <a>Home</a><span class="checked"></span>
</div>
</li>
<li>
<div class="ec-sidebar-block-item">
<input type="checkbox" name="category[]" value="Heavy Equipment" <?php echo (isset($_POST['category']) && in_array('Heavy Equipment', $_POST['category'])) ? 'checked' : ''; ?> /> <a>Heavy Equipment</a><span class="checked"></span>
</div>
</li>
<!-- Add similar blocks for other categories -->
<li>
<div>
<label for="minPrice">Min Price:</label>
<input type="number" name="minPrice" id="minPrice" value="<?php echo isset($_POST['minPrice']) ? $_POST['minPrice'] : (isset($_GET['minPrice']) ? $_GET['minPrice'] : ''); ?>" />
</div>
</li>
<li>
<div>
<label for="maxPrice">Max Price:</label>
<input type="number" name="maxPrice" id="maxPrice" value="<?php echo isset($_POST['maxPrice']) ? $_POST['maxPrice'] : (isset($_GET['maxPrice']) ? $_GET['maxPrice'] : ''); ?>" />
</div>
</li>
</ul>
<button type="submit">Apply</button>
</form>
</ul>
</div>
</div>
<!-- Sidebar Price Block -->
<div class="ec-sidebar-block">
<div class="ec-sb-title">
<h3 class="ec-sidebar-title">Price</h3>
</div>
<div class="ec-sb-block-content es-price-slider">
<div class="ec-price-filter">
<div id="ec-sliderPrice" class="filter__slider-price" data-min="0" data-max="250" data-step="10"></div>
<div class="ec-price-input">
<label class="filter__label"><input type="text" class="filter__input"></label>
<span class="ec-price-divider"></span>
<label class="filter__label"><input type="text" class="filter__input"></label>
</div>
</div>
</div>
</div>
<!-- Sidebar Size Block -->
<!-- <div class="ec-sidebar-block">
<div class="ec-sb-title">

View File

@ -1,5 +1,5 @@
<?php
include "functions.php";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);