I am trying to create an e-commerce website and on my products page, I want to be able to sort the products by categories (eg mens, women, children etc). I have managed to make the category names show up dynamically in the filter option.. But I dont know how to get the "apply" button to work. So once I pick the category and click apply it redirects me to a page showing only those products that are in a selected category.
Here is the code I have used:
function get_categories_new(){
$query = query("SELECT * FROM categories");
confirm($query);
while($row = fetch_array($query)) {
$categories_links = <<<DELIMETER
"<option> {$row['cat_title']}</option></a>"
DELIMETER;
echo $categories_links;
}
}
I am then calling the function in the products page. Like this:
<!-- FILTER -->
<section class="module-small">
<div class="container">
<form class="row">
<div class="col-sm-3 m-b-sm-20">
<select class="form-control">
<option selected="selected"> Choose Category</option>
<?php get_categories_new(); ?>
</select>
</div>
<div class="col-sm-3">
<button type="submit" class="btn btn-block btn-round btn-g">Apply</button>
</div>
</form>
</div>
</section>
<!-- /FILTER -->
Use form action to some target page and get the user submitted data via GET method. Then do the filteration. 1. form action="products-filter.php"
in form tag add action tag ex. to "products-filter.php" 2. select class="form-control" name="category"
in select dropdown add the name as "category". Using this we will retrieve the data and do the filteration.
products-filter.php code
<?php
function get_products_by_category($category_name = ''){
$query = query("SELECT * FROM products where category_name ='$category_name'");
echo "<table>";
while($row = fetch_array($query)) {
echo "<tr><td>".$row['product_name']."</td></tr>";
}
echo "</table>";
}
get_products_by_category(add_slashes($_GET['category']));
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.