How to create Price Range Slider in jQuery & PHP with MySQL
Mysql 01-Mar-2019

How to create Price Range Slider in jQuery & PHP with MySQL

I have received many requests from my readers to create a price range slider, this article going to solve there problems so basically I used jQuery UI to perform this tutorial base which is jQuery and created a PHP and MySQL settings for ease of PHP Programmers so let see how it works and how to configure it in your websites. Mostly Developers use this slider in eCommerce sites where people can search products with price range.

Database Details:

database name => phpgang
table name => like

Database file run in your MySQL to create database and add data in table.

-- Table structure for table `products`

CREATE TABLE `products` (
  `product` text NOT NULL,
  `price` float(5,2) NOT NULL,
  `status` int(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)

-- Dumping data for table `products`

INSERT INTO `products` VALUES (1, 'Product # 1', 70.54, '2014-01-06 03:20:23', 1);
INSERT INTO `products` VALUES (2, 'Product # 2', 300.00, '2014-01-06 03:20:23', 1);
INSERT INTO `products` VALUES (3, 'Product # 3', 805.88, '2014-01-06 03:20:42', 1);


Edit this file as per your database credentials, I used MySQLi as you know MySQL is deprecated in latest version and will be removed. I suggest all developers to user MySQLi in your PHP applications instead of MySQL.

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
$connection = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);


Contains HTML and PHP code to select records from database and show the products those come in the rang of given price.

    $values = str_replace(' ','',$_POST['amount']);
    $values = str_replace('$','',$values);
    $values = explode('-',$values);
    $min = $values[0];
    $max = $values[1];
    $res = mysqli_query($connection,'select `id`,`product`,`price`, DATE_FORMAT(`date`,"%D %b-%Y") as `date` from products where `price` BETWEEN "'.$min.'" AND "'.$max.'"');
    $count  =   mysqli_num_rows($res);
    if($count > 0)
            $id         = $row['id'];
            $product    = $row['product'];
            $price      = $row['price'];
            $date       = $row['date'];

            $HTML .= '<div>';
            $HTML .= 'Product ID: '.$id;
            $HTML .= '<br />Product Name: '.$product;
            $HTML .= '<br />Price: <strong>$'.$price.'</strong> Posted on: '.$date;
            $HTML .= '</div><br /><hr />';
        $HTML='No Data Found';
    $min = 30;
    $max = 700;
    $HTML='Search Products in range.';

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="" xml:lang="en" lang="en">
    <title>How to create Price Range Slider in jQuery & PHP with MySQL |</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <style type="text/css">
      img {border-width: 0}
      * {font-family:'Lucida Grande', sans-serif;}
    <script type="text/javascript" src="jquery-1.8.0.min.js"></script> 
    <link rel="stylesheet" href="" />
    <script src=""></script>

<script type="text/javascript">
$(function() {
    $( "#slider-range" ).slider({
      range: true,
      min: 0,
      max: 1000,
      values: [ <?php echo $min; ?>, <?php echo $max; ?> ],
      slide: function( event, ui ) {
        $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
    $( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
      " - $" + $( "#slider-range" ).slider( "values", 1 ) );
      <h2>How to create Price Range Slider in jQuery & PHP with MySQL Example&nbsp;&nbsp;&nbsp;=> <a href="">Home</a> | <a href="">More Demos</a></h2>

<form action="" method="post" id="products">
<div style="margin-left:20px">
    <label for="amount">Price range:</label>
    <input type="text" id="amount" name="amount" style="border:0; color:#f6931f; font-weight:bold;" readonly>
    <div id="slider-range" style="width:80%;"></div>
    <input type="submit" value=" Show products " />
    <?php echo $HTML; ?>


In above code there is a range limit given min: 0max: 1000, minimum is 0 and maximum is 1000 you can edit it as per your requirement and also can make it dynamic like in database you have product with min price and product with max price both are on your websites requirement. values: [ <?php echo $min; ?>, <?php echo $max; ?> ] this jQuery line used to show current selected rage and in this tutorial i have selected 30 to 700 and you can chose your own.

In PHP code i used to get products from database in selected rage in demo I added 3 dummy products with different values.

<script type="text/javascript" src="jquery-1.8.0.min.js"></script> 
<link rel="stylesheet" href="" />
<script src=""></script>

jQuery UI library and jQuery library required to perform Slider navigation.