product search filter in php

product search filter in php

product search filter in php : -

in this post we learn how to filter product  for brand wise size wise and colour wise  and much more  using php mysql and jquery it is best tutorial  for developer want to
develop e-Commerce  website save much time to implement product filter and next post we comming with product filter with ajax and price range slider

follow the following simple stap and set product filter in your project

step first create database phpcooker_script

and import these table : -


--
-- Database: `phpcooker_script`
--

-- --------------------------------------------------------

--
-- Table structure for table `product`
--

CREATE TABLE IF NOT EXISTS `product` (
  `product_id` int(20) NOT NULL AUTO_INCREMENT,
  `product_category_id` int(10) NOT NULL,
  `product_sku` varchar(80) NOT NULL,
  `product_name` varchar(120) NOT NULL,
  `product_brand` varchar(100) NOT NULL,
  `product_price` decimal(8,2) NOT NULL,
  `product_size` char(5) NOT NULL,
  `product_color` varchar(50) NOT NULL,
  `product_image` varchar(100) NOT NULL,
  `product_quantity` mediumint(5) NOT NULL,
  `product_status` enum('0','1') NOT NULL COMMENT '0-active,1-inactive',
  PRIMARY KEY (`product_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `product`
--

INSERT INTO `product` (`product_id`, `product_category_id`, `product_sku`, `product_name`, `product_brand`, `product_price`, `product_size`, `product_color`, `product_image`, `product_quantity`, `product_status`) VALUES
(1, 1, 'FRD00F1', 'Sangria Collar 3/4Th Sleeves Embroidered Flared Anarkali', 'Sangria', '1750.00', 'M', 'Black', 'Sangria-Collar.jpg', 2, '1'),
(2, 1, 'FRD00F3', 'Aurelia Navy Blue Printed Cotton Kurta', 'Aurelia', '900.00', 'L', 'Blue', 'Aurelia-Navy-Blue-Printed-Cotton-Kurta.jpg', 1, '1'),
(3, 1, 'RO100', 'Biba White Printed Viscose Kurta', 'Biba', '900.00', 'S', 'White', 'Biba-White-Printed-Viscose-Kurta.jpg', 3, '1'),
(4, 1, 'HND0E1', 'Aurelia Yellow Printed Cotton Kurta', 'Aurelia', '700.00', 'M', 'Yellow', 'Aurelia-Yellow-Printed-Cotton-Kurta.jpg', 2, '1'),
(5, 1, 'MST0G1', 'Biba Blue Printed Kurta', 'Biba', '1040.00', 'S', 'Blue', 'Biba-Blue-Printed-Kurta.jpg', 5, '1'),
(6, 1, 'NS0A1', 'Aurelia Navy Blue Printed Cotton Kurta', 'Aurelia', '699.00', 'M', 'Blue', 'Aurelia-Navy-Blue-Printed-Cotton-Kurta1.jpg', 1, '1'),
(7, 1, 'NS2S1', 'Sangria Round Neck 3/4Th Sleeves Anarkali', 'Sangria', '999.00', 'M', 'Black', 'Sangria-Round-Neck-3-4Th-Sleeves-Anarkali-8650-797031003-1-catalog_s.jpg', 4, '1'),
(8, 1, 'HND1A2', 'Biba Dark Grey Solid Cotton Kurta', 'Biba', '1320.00', 'L', 'Black', 'Biba-Dark-Grey-Solid-Cotton-Kurta-1317-2851182-1-catalog_s.jpg', 1, '1'),
(9, 1, 'DGE0C1', 'Sangria Henley Neck Full Sleeves Printed Anarkali', 'Sangria', '650.00', 'L', 'White', 'Sangria-Henley-Neck-Full-Sleeves-Printed-Anarkali-6253-155890003-1-catalog_s.jpg', 3, '1'),
(10, 1, 'FRD01T3', 'Rangmanch By Pantaloons Off White Solid Cotton Blend Kurta', 'Rangmanch', '800.00', 'M', 'White', 'Rangmanch-By-Pantaloons-Off-White-Solid-Cotton-Blend-Kurta-7882-762960003-1-catalog_s.jpg', 3, '1');

 

stap second create conf.php and put  this code


<?php
$conn = @mysql_connect("localhost","root","");
mysql_select_db("phpcooker_script",$conn);
?>

step third create index.php and put blew code


<?php include"conf.php";
//declaration array varible
$colour = array();
$brand = array();
$size = array();

//finding query string value
if(isset($_REQUEST['colour'])){
 //query string value to array and removing empty index of array
 $colour = array_filter(explode("-",$_REQUEST['colour']));
}

if(isset($_REQUEST['brand'])){
 
 $brand = array_filter(explode("-",$_REQUEST['brand']));
}

if(isset($_REQUEST['size'])){
 
 $size = array_filter(explode("-",$_REQUEST['size']));
}

?>
<!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">
 <meta name="description" content="">
 <meta name="author" content="">

 <title>Product filter in php</title>

 <!-- Bootstrap Core CSS -->
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

 <!-- Custom CSS -->
 <link href="css/style.css" rel="stylesheet">

</head>

<body>



 <!-- Page Content -->
 <div class="container" style="padding-top:2%;" >

 <div class="row">
 <div class="col-md-3">
 <p class="lead">Product filter</p>
    
 <div class="list-group">
    <h3>Colour</h3>
    <?php
     $query = "select distinct(product_color) from product where product_status = '1'"; 
     $rs = mysql_query($query,$conn) or die("Error : ".mysql_error());
     while($color_data = mysql_fetch_assoc($rs)){
    
    ?>
 <a href="javascript:void(0);" class="list-group-item">
     <input type="checkbox" class="item_filter colour" value="<?php echo $color_data['product_color']; ?>" <?php if(in_array($color_data['product_color'],$colour)){ echo"checked"; } ?> >
      <?php echo $color_data['product_color']; ?></a>
    <?php } ?> 
 </div>
    
    
    <div class="list-group">
    <h3>Brand</h3>
    <?php
     $query = "select distinct(product_brand) from product where product_status = '1'"; 
     $rs = mysql_query($query,$conn) or die("Error : ".mysql_error());
     while($brand_data = mysql_fetch_assoc($rs)){
    
    ?>
 <a href="javascript:void(0);" class="list-group-item">
     <input type="checkbox" class="item_filter brand" value="<?php echo $brand_data['product_brand']; ?>" <?php if(in_array($brand_data['product_brand'],$brand)){ echo"checked"; } ?> >
      <?php echo $brand_data['product_brand']; ?></a>
    <?php } ?> 
 </div>
    
    <div class="list-group">
    <h3>Size</h3>
    <?php
     $query = "select distinct(product_size) from product where product_status = '1'"; 
     $rs = mysql_query($query,$conn) or die("Error : ".mysql_error());
     while($size_data = mysql_fetch_assoc($rs)){
    
    ?>
 <a href="javascript:void(0);" class="list-group-item">
     <input type="checkbox" class="item_filter size" value="<?php echo $size_data['product_size']; ?>" <?php if(in_array($size_data['product_size'],$size)){ echo"checked"; } ?> >
      <?php echo $size_data['product_size']; ?></a>
    <?php } ?> 
 </div>
    
    
 </div>

 <div class="col-md-9">

 
 <div class="row">
 
     <?php
     $query = "select * from product where product_status = '1'";
 //filter query start
      if(!empty($colour)){
       $colordata =implode("','",$colour);
       $query .= " and product_color in('$colordata')";
      }
      
      if(!empty($brand)){
       $branddata =implode("','",$brand);
       $query .= " and product_brand in('$branddata')";
      }
      
      if(!empty($size)){
       $sizedata =implode("','",$size);
       $query .= " and product_size in('$sizedata')";
      }
 //filter query end
     
     $rs = mysql_query($query,$conn) or die("Error : ".mysql_error());
     
      while($product_data = mysql_fetch_assoc($rs)){
     ?>
 <div class="col-sm-4 col-lg-4 col-md-4">
 <div class="thumbnail">
 <img src="image/<?php echo $product_data['product_image']; ?>" alt="">
 <div class="caption">
 
 <p><strong><a href="#"><?php echo $product_data['product_name']; ?></a>
 </strong></p>
         <h4 style="text-align:center;" >$ <?php echo $product_data['product_price']; ?></h4>
 <p>Color : <?php echo $product_data['product_color']; ?>     Brand : <?php echo $product_data['product_brand']; ?> </p>
        
        <p>Size : <?php echo $product_data['product_size']; ?>     Quantity : <?php echo $product_data['product_quantity']; ?> </p>
        
 </div>
 <div class="ratings">
 <p class="pull-right">15 reviews</p>
 <p>
 <span class="glyphicon glyphicon-star"></span>
 <span class="glyphicon glyphicon-star"></span>
 <span class="glyphicon glyphicon-star"></span>
 <span class="glyphicon glyphicon-star"></span>
 <span class="glyphicon glyphicon-star"></span>
 </p>
 </div>
 </div>
 </div>
      <?php } ?>
 
 
 </div>

 </div>

 </div>

 </div>
 <!-- /.container -->

 <div class="container">

 <hr>

 <!-- Footer -->
 <footer>
 <div class="row">
 <div class="col-lg-12">
 <p>Copyright &copy; PHP Cooker 2017</p>
 </div>
 </div>
 </footer>

 </div>
 <!-- /.container -->

 <!-- jQuery -->
 <script src="js/jquery.js"></script>

 <script>
 $(function(){
  $('.item_filter').click(function(){
   var colour = multiple_values('colour');
   var brand = multiple_values('brand');
   var size = multiple_values('size');
   
   var url ="index.php?colour="+colour+"&brand="+brand+"&size="+size;
   window.location=url;
  });
  
 });
 
 
 function multiple_values(inputclass){
  var val = new Array();
  $("."+inputclass+":checked").each(function() {
   val.push($(this).val());
  });
 return val.join('-');
}
 
 </script>
 
 
 <!-- Bootstrap Core JavaScript -->
 <script src="js/bootstrap.min.js"></script>

</body>

</html>


step fourth create css folder / style.css and put code



/*!
 * Start Bootstrap - Shop Homepage (http://startbootstrap.com/)
 * Copyright 2013-2016 Start Bootstrap
 * Licensed under MIT (https://github.com/BlackrockDigital/startbootstrap/blob/gh-pages/LICENSE)
 */

body {
    padding-top: 70px; /* Required padding for .navbar-fixed-top. Remove if using .navbar-static-top. Change if height of navigation changes. */
}

.slide-image {
    width: 100%;
}

.carousel-holder {
    margin-bottom: 30px;
}

.carousel-control,
.item {
    border-radius: 4px;
}

.caption {
    height: 160px;
    overflow: hidden;
}

.caption h4 {
    white-space: nowrap;
}

.thumbnail img {
    width: 100%;
}

.ratings {
    padding-right: 10px;
    padding-left: 10px;
    color: #d17581;
}

.thumbnail {
    padding: 0;
}

.thumbnail .caption-full {
    padding: 9px;
    color: #333;
}

footer {
    margin: 50px 0;
}

 

and enjoy  smileysmileysmileysmileysmiley

 

 

Demo

Download Hare

Total Coments (7)

  1. Rahid Khan
    • 12 Jan 2018

    i can't implement can u please help me ?

  2. Rahid Khan
    • 16 Jan 2018

    i got an nothing just error "No Keywords match ! Try Again.. " help me out this problem

  3. Peter
    • 03 Feb 2018

    When there's a colour with an '-' in it, the script doesn't get that value Like: Off-white

  4. drek
    • 15 Mar 2018

    how to add searching box by running with this

  5. Dhruv
    • 21 Mar 2018

    Thanks..

  6. Milind Bhuvad
    • 20 Jul 2018

    Nice Demo to filter product. It's Worked form me.

  7. Yashraj Waghe
    • 01 Aug 2018

    Very Good.... Thank You....