Online Shopping Products Filter Using Ajax & PHP

Here we going to see how to filter the product which would you like within your price range, brand & colors. Most of the online shopping website required the products filter, which helps the user to customize there specific product with the price range or brands. If you see the e-commerce website they are a huge amount of products, that is difficult to search your required product, so this product filter reduces your time to get the product which most you like. In this article, we show you how to filter products using Ajax & PHP. With good user interface here you have the product filter, you can use this for any kind of e-commerce project websites. Here you have a different type of filter option in this shopping example and you can see live example below in the article.
Using Ajax and PHP the online shopping products filter used. Without refreshing the page you can filter the products which would you like. In the left sidebar, you have the option to filter the price range and checkbox of brands and colors. This live product filter is run based on Ajax which only used most of the e-commerce websites. For fetching the product the PHP is used. The below example code will help to integrate this filter to your e-commerce project.
Database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
-- -- Database: `product_filter` -- -- -------------------------------------------------------- -- -- Table structure for table `product` -- CREATE TABLE `product` ( `product_id` int(20) NOT NULL, `product_name` varchar(120) NOT NULL, `product_brand` varchar(100) NOT NULL, `product_price` decimal(8,2) NOT NULL, `product_color` varchar(255) NOT NULL, `gender` varchar(10) NOT NULL, `product_image` varchar(100) NOT NULL, `product_quantity` mediumint(5) NOT NULL, `stock_avail` enum('0','1') NOT NULL COMMENT '0-active,1-inactive' ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- -- Dumping data for table `product` -- INSERT INTO `product` (`product_id`, `product_name`, `product_brand`, `product_price`, `product_color`, `gender`, `product_image`, `product_quantity`, `stock_avail`) VALUES (1, 'Women\'s Casual Short Sleeve Dresses', 'Lark & Ro', '44.00', 'White', 'Female', 'product-1.jpg', 50, '1'), (2, 'Men\'s Short-Sleeve Indigo Round-Neck Pocket T-Shirt', 'Polo Ralph Lauren', '18.00', 'Green', 'Male', 'product-2.jpg', 50, '1'), (3, 'Fantastic Zone Women\'s Casual Summer Dresses', 'Eliacher', '90.00', 'Purple', 'Female', 'product-3.jpg', 50, '1'), (4, 'Men\'s \"The Perfect V-Neck T-Shirt\" Short-Sleeve ', 'Tommy Hilfiger', '29.00', 'Brown', 'Male', 'product-4.jpg', 50, '1'), (5, 'Women\'s Short Sleeve Shirts Loose Casual', 'Urban CoCo', '19.00', 'White', 'Female', 'product-5.jpg', 50, '1'), (6, ' 1 Button Center Vent Wool Blend Blazer Jacket', 'Tommy Hilfiger', '190.00', 'Brown', 'Male', 'product-6.jpg', 50, '1'), (7, 'Bell Sleeve Shirt Tie Knot Casual Blouses Tops', 'oxiuly', '62.00', 'Blue', 'Female', 'product-7.jpg', 50, '1'), (8, 'Black Men\'s Assorted Crew T-Shirt', 'Lee', '29.00', 'Black', 'Male', 'product-8.jpg', 50, '1'), (9, 'Womens Waffle Knit Tunic Blouse Tie Knot Short', 'Lark & Ro', '21.00', 'Black', 'Female', 'product-9.jpg', 50, '1'), (10, 'Jeans Distressed Slim Fit Ripped Jeans Comfy Stretch ', 'Urban CoCo', '29.00', 'Blue', 'Female', 'product-10.jpg', 50, '1'), (11, 'Men\'s 2 Button Slim Fit Suit with Hemmed Pant', 'Lee', '126.00', 'Brown', 'Male', 'product-11.jpg', 50, '1'), (12, 'Pappagallo Women\'s The Carrie Dress\r\n', 'Milumia', '64.00', 'Black', 'Female', 'product-12.jpg', 50, '1'); -- -- Indexes for dumped tables -- -- -- Indexes for table `product` -- ALTER TABLE `product` ADD PRIMARY KEY (`product_id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `product` -- ALTER TABLE `product` MODIFY `product_id` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15; COMMIT; |
conn.php
1 2 3 4 5 6 7 8 |
<?php $host = "localhost"; $db_user = "root"; $db_pass = ""; $dbname = "product_filter"; $connect = new PDO("mysql:host=$host; dbname=$dbname", $db_user, $db_pass); ?> |
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
<?php include('conn.php'); //Database Connection ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Online Shopping Products Filter using Ajax & PHP</title> <link href="css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" /> <link href="css/jquery-ui.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet"> </head> <body> <!-- Page Content --> <div class="container"> <br/> <br/> <h1 class="text-center">AOX - Online Shopping Products Filter using Ajax & PHP</h1> <br/> <br/> <div class="row"> <div class="col-md-3"> <div class="list-group"> <h3>Price</h3> <input type="hidden" id="min_price_hide" value="0" /> <input type="hidden" id="max_price_hide" value="300" /> <p id="price_show">$10 - $300</p> <div id="price_range"></div> </div> <div class="list-group"> <h3>Gender</h3> <?php $query = " SELECT DISTINCT(gender) FROM product WHERE stock_avail = '1' ORDER BY gender DESC "; $statement = $connect->prepare($query); $statement->execute(); $result = $statement->fetchAll(); foreach($result as $row) { ?> <div class="list-group-item checkbox"> <label> <input type="checkbox" class="filter_all gender" value="<?php echo $row['gender']; ?>"> <?php echo $row['gender']; ?> </label> </div> <?php } ?> </div> <div class="list-group"> <h3>Brand</h3> <?php $query = " SELECT DISTINCT(product_brand) FROM product WHERE stock_avail = '1' ORDER BY product_brand DESC "; $statement = $connect->prepare($query); $statement->execute(); $result = $statement->fetchAll(); foreach($result as $row) { ?> <div class="list-group-item checkbox"> <label> <input type="checkbox" class="filter_all brand" value="<?php echo $row['product_brand']; ?>"> <?php echo $row['product_brand']; ?> </label> </div> <?php } ?> </div> <div class="list-group"> <h3>Color</h3> <?php $query = " SELECT DISTINCT(product_color) FROM product WHERE stock_avail = '1' ORDER BY product_color DESC "; $statement = $connect->prepare($query); $statement->execute(); $result = $statement->fetchAll(); foreach($result as $row) { ?> <div class="list-group-item checkbox"> <label> <input type="checkbox" class="filter_all color" value="<?php echo $row['product_color']; ?>"> <?php echo $row['product_color']; ?> </label> </div> <?php } ?> </div> </div> <div class="col-md-9"> <div class="row filter_data"> </div> </div> </div> </div> <script src="js/jquery-1.11.1.min.js"></script> <script src="js/bootstrap.min.js"></script> <script src="js/jquery-ui.js"></script> <script> $(document).ready(function() { filter_data(); function filter_data() { $('.filter_data'); var action = 'fetch_data'; var minimum_price = $('#min_price_hide').val(); var maximum_price = $('#max_price_hide').val(); var brand = get_filter('brand'); var color = get_filter('color'); var gender = get_filter('gender'); $.ajax({ url: "fetch.php", method: "POST", data: { action: action, minimum_price: minimum_price, maximum_price: maximum_price, brand: brand, color: color, gender: gender }, success: 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(); }); $('#price_range').slider({ range: true, min: 10, max: 300, values: [10, 300], step: 10, stop: function(event, ui) { $('#price_show').html(ui.values[0] + ' - ' + ui.values[1]); $('#min_price_hide').val(ui.values[0]); $('#max_price_hide').val(ui.values[1]); filter_data(); } }); }); </script> </body> </html> |
fetch.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<?php include('conn.php'); //Database Connection if (isset($_POST["action"])) { $query = " SELECT * FROM product WHERE stock_avail = '1' "; if (isset($_POST["minimum_price"], $_POST["maximum_price"]) && !empty($_POST["minimum_price"]) && !empty($_POST["maximum_price"])) { $query .= " AND product_price BETWEEN '" . $_POST["minimum_price"] . "' AND '" . $_POST["maximum_price"] . "' "; } if (isset($_POST["brand"])) { $brand_filter = implode("','", $_POST["brand"]); $query .= " AND product_brand IN('" . $brand_filter . "') "; } if (isset($_POST["color"])) { $color_filter = implode("','", $_POST["color"]); $query .= " AND product_color IN('" . $color_filter . "') "; } if (isset($_POST["gender"])) { $gender_filter = implode("','", $_POST["gender"]); $query .= " AND gender IN('" . $gender_filter . "') "; } $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="image/' . $row['product_image'] . '"> <img class="pic-2" src="image/' . $row['product_image'] . '"> </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['product_name'] . '</a></h3> <div class="price"> $' . $row['product_price'] . ' <span>$' . $row['product_price'] . '</span> </div> <p>Gender : ' . $row['gender'] . ' <br /> Brand : ' . $row['product_brand'] . ' <br /> Color: ' . $row['product_color'] . ' </p> <a class="add-to-cart" href="">ADD TO CART</a> </div> </div> </div>'; } } else { $output = '<h3>No Data Found</h3>'; } echo $output; } ?> |
style.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
.product-grid, .product-grid .product-image4 { position: relative } .product-grid { font-family: Poppins, sans-serif; text-align: center; border-radius: 3px; overflow: hidden; z-index: 1; transition: all .3s ease 0s; margin-bottom: 20px; } .product-grid:hover { box-shadow: 0 0 10px rgba(0, 0, 0, .2) } .product-grid .product-image4 a { display: block } .product-grid .product-image4 img { width: 100%; height: auto } .product-grid .pic-1 { opacity: 1; transition: all .5s ease-out 0s } .product-grid:hover .pic-1 { opacity: 0 } .product-grid .pic-2 { position: absolute; top: 0; left: 0; opacity: 0; transition: all .5s ease-out 0s } .product-grid:hover .pic-2 { opacity: 1 } .product-grid .social { width: 180px; padding: 0; margin: 0 auto; list-style: none; position: absolute; right: 0; left: 0; top: 50%; transform: translateY(-50%); transition: all .3s ease 0s } .product-grid .social li { display: inline-block; opacity: 0; transition: all .7s } .product-grid .social li:nth-child(1) { transition-delay: .15s } .product-grid .social li:nth-child(2) { transition-delay: .3s } .product-grid .social li:nth-child(3) { transition-delay: .45s } .product-grid:hover .social li { opacity: 1 } .product-grid .social li a { color: #222; background: #fff; font-size: 17px; line-height: 36px; width: 40px; height: 36px; border-radius: 2px; margin: 0 5px; display: block; transition: all .3s ease 0s } .product-grid .social li a:hover { color: #fff; background: #0b0d1e } .product-grid .social li a:after, .product-grid .social li a:before { content: attr(data-tip); color: #fff; background-color: #000; font-size: 12px; line-height: 20px; border-radius: 3px; padding: 0 5px; white-space: nowrap; opacity: 0; transform: translateX(-50%); position: absolute; left: 50%; top: -30px } .product-grid .social li a:after { content: ''; height: 15px; width: 15px; border-radius: 0; transform: translateX(-50%) rotate(45deg); top: -22px; z-index: -1 } .product-grid .social li a:hover:after, .product-grid .social li a:hover:before { opacity: 1 } .product-grid .product-discount-label, .product-grid .product-new-label { color: #fff; background-color: #0b0d1e; font-size: 13px; font-weight: 800; text-transform: uppercase; line-height: 45px; height: 45px; width: 45px; border-radius: 50%; position: absolute; left: 10px; top: 15px; transition: all .3s } .product-grid .product-discount-label { left: auto; right: 10px; background-color: #5a45ff; } .product-grid:hover .product-new-label { opacity: 0 } .product-grid .product-content { padding: 25px } .product-grid .title { font-size: 15px; font-weight: 400; text-transform: capitalize; margin: 0 0 7px; transition: all .3s ease 0s } .product-grid .title a { color: #222 } .product-grid .title a:hover { color: #0b0d1e } .product-grid .price { color: #0b0d1e; font-size: 17px; font-weight: 700; margin: 0 2px 15px 0; display: block } .product-grid .price span { color: #909090; font-size: 13px; font-weight: 400; letter-spacing: 0; text-decoration: line-through; text-align: left; vertical-align: middle; display: inline-block } .product-grid .add-to-cart { border: 1px solid #e5e5e5; display: inline-block; padding: 10px 20px; color: #888; font-weight: 600; font-size: 14px; border-radius: 4px; transition: all .3s } .product-grid:hover .add-to-cart { border: 1px solid transparent; background: #0b0d1e; color: #fff } .product-grid .add-to-cart:hover { background-color: #505050; box-shadow: 0 0 10px rgba(0, 0, 0, .5) } @media only screen and (max-width: 990px) { .product-grid { margin-bottom: 30px } } /* SideBar */ #price_range { height: 6px; } .ui-slider-handle { height: 13px !important; width: 13px !important; background: #0b0d1e !important; border-radius: 25px; } .ui-slider-range.ui-corner-all.ui-widget-header { background: #5a45ff; } .list-group { border: 1px solid #f3f3f3; padding: 10px; margin-bottom: 10px; border-radius: 3px; } .list-group h3 { font-size: 22px; } .list-group-item { border: none; margin-left: -10px; margin-bottom: -20px; font-size: 16px; } .list-group-item:focus, .list-group-item:hover { z-index: 0; } |
EcommerceOnline ShoppingOnline Store
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]