Search Filter

This commit is contained in:
Jun Barroga 2024-02-21 18:08:17 +08:00
parent 68ac058e9c
commit 5ebf8bae4e
2 changed files with 114 additions and 65 deletions

View File

@ -0,0 +1,26 @@
<?php
// 02-21-2024 Jun Jihad Search Filter Action
$categories = isset($_POST['category']) ? $_POST['category'] : array();
$categoryFinal = null;
$count=0;
foreach ($categories as $category) {
// Perform checks or actions for each category
// For example:
$categoriesCopy = $categoryFinal;
$categoryFinal =$categoryFinal!== null? $categoriesCopy . '&category['.$count.']='.$category:$categoriesCopy . 'category['.$count.']='.$category;
$count+=1;
echo "Processing category: $category <br>";
// You can add your logic here
}
$minPrice = isset($_POST['minPrice']) ? $_POST['minPrice'] : null;
$maxPrice = isset($_POST['maxPrice']) ? $_POST['maxPrice'] : null;
if(isset($categoryFinal)){
header("location: shop-list-left-sidebar.php?$categoryFinal&minPrice=$minPrice&maxPrice=$maxPrice");
}else{
header("location: shop-list-left-sidebar.php?&minPrice=$minPrice&maxPrice=$maxPrice");
}
// 02-21-2024 Jun Jihad Search Filter Action

View File

@ -9,20 +9,13 @@ if ($_SESSION["userId"] <> "") {
$_SESSION["isLoggedIn"] = false;
}
$productSearchResult = $_SESSION["productSearchResult"];
// Filter products based on selected categories
// $category = strtolower(trim($_GET['category']));
// 02-19-2024 Jun Jihad Search Filter
$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) {
@ -31,17 +24,12 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
$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) &&
@ -56,12 +44,10 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
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;
}
@ -73,71 +59,62 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
}
}
}
} // 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 = [];
$minPrice = isset($_GET['minPrice']) && $_GET['minPrice'] !== '' ? floatval($_GET['minPrice']) : null;
$maxPrice = isset($_GET['maxPrice']) && $_GET['maxPrice'] !== '' ? floatval($_GET['maxPrice']) : null;
// Get minPrice and maxPrice from the URL
$minPrice = isset($_GET['minPrice']) ? floatval($_GET['minPrice']) : null;
$maxPrice = isset($_GET['maxPrice']) ? floatval($_GET['maxPrice']) : null;
echo "Min Price: " . ($minPrice !== null ? $minPrice : "null") . "<br>";
echo "Max Price: " . ($maxPrice !== null ? $maxPrice : "null") . "<br>";
// 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'];
if ($minPrice === null) {
$minPrice = 0;
}
// Check if the product matches the selected category and falls within the price range
if ($maxPrice === null) {
$maxPrice = PHP_FLOAT_MAX;
}
if (
$productCategory == $category &&
($productPrice >= $minPrice) &&
($productPrice <= $maxPrice)
($minPrice === null || $productPrice >= $minPrice) &&
($maxPrice === null || $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'];
if ($minPrice === null) {
$minPrice = 0;
}
// Check if the product falls within the price range
if (($productPrice >= $minPrice) &&
($productPrice <= $maxPrice)
if ($maxPrice === null) {
$maxPrice = PHP_FLOAT_MAX;
}
if (($minPrice === null || $productPrice >= $minPrice) &&
($maxPrice === null || $productPrice <= $maxPrice)
) {
$filteredProducts[] = $result;
}
}
}
} else {
// If no filter parameters are present, display all products
$filteredProducts = $productSearchResult['results'];
}
?>
<!DOCTYPE html>
<html lang="en">
@ -281,7 +258,6 @@ if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['cate
</div>
<!-- Shop Top End -->
<!-- Shop content Start -->
<div class="shop-pro-content">
<div class="shop-pro-inner list-view">
@ -342,22 +318,15 @@ if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['cate
<ul class="ec-pro-pagination-inner">
<?php
// Calculate the total number of pages
$totalPages = ceil(count($filteredProducts) / $productsPerPage);
// Define the maximum number of pagination links to display
$maxPaginationLinks = 5;
// Calculate the range of pagination links to display
$startPage = max(1, $currentPage - floor($maxPaginationLinks / 2));
$endPage = min($startPage + $maxPaginationLinks - 1, $totalPages);
// Display the first page link
if ($startPage > 1) {
?>
<li><a href="?page=1">1</a></li>
<?php
// Add an ellipsis if there are more pages before the first displayed page
if ($startPage > 2) {
?>
<li><span>...</span></li>
@ -365,25 +334,21 @@ if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['cate
}
}
// 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)));
$paginationUrl = '?' . http_build_query(array_merge($_GET, $_POST, array('page' => $page)));
// Output the pagination link
?>
<li><a class="<?php echo $activeClass; ?>" href="<?php echo $paginationUrl; ?>"><?php echo $page; ?></a></li>
<?php
}
// Display the last page link
if ($endPage < $totalPages) {
// Add an ellipsis if there are more pages after the last displayed page
if ($endPage < $totalPages - 1) {
?>
<li><span>...</span></li>
@ -414,7 +379,68 @@ if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['cate
<h3 class="ec-sidebar-title">Category</h3>
</div>
<div class="ec-sb-block-content">
<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']))); ?>">
<form method="POST" action="shop-list-left-sidebar-action.php">
<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'])) || (isset($_GET['category']) && in_array('Electronics', $_GET['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'])) || (isset($_GET['category']) && in_array('Solar', $_GET['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'])) || (isset($_GET['category']) && in_array('E-bike', $_GET['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'])) || (isset($_GET['category']) && in_array('E-vehicle', $_GET['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'])) || (isset($_GET['category']) && in_array('Appliance', $_GET['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'])) || (isset($_GET['category']) && in_array('Smart Home', $_GET['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'])) || (isset($_GET['category']) && in_array('Home', $_GET['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'])) || (isset($_GET['category']) && in_array('Heavy Equipment', $_GET['category'])) ? 'checked' : ''; ?> /> <a>Heavy Equipment</a><span class="checked"></span>
</div>
</li>
<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>
<!-- <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] . '?' . http_build_query(array_merge($_GET, $_POST)); ?>">
<ul>
<li>
<div class="ec-sidebar-block-item">
@ -456,8 +482,6 @@ if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['cate
<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>
@ -472,12 +496,11 @@ if (!empty($_GET['minPrice']) || !empty($_GET['maxPrice']) || !empty($_GET['cate
</li>
</ul>
<button type="submit">Apply</button>
</form>
</form> -->
</div>
</div>
<!-- 02-19-2024 Jun Jihad Search Filter -->
<!-- Sidebar Size Block -->
<!-- <div class="ec-sidebar-block">
<div class="ec-sb-title">