You are here:  » Dynamic Price range accroding to filter


Dynamic Price range accroding to filter

Submitted by technoarenasol on Thu, 2012-10-18 11:17 in

Hi David

I add price slider in sidebar in {link saved} I give minimum and maximum price manually....But I want to change price range according filter means when any one click on Samsung so slider price range will be change ......minimum and maximum price retrieve from database ...can be possible

Submitted by support on Thu, 2012-10-18 11:58

Hi technoarenasol,

You can dynamically query the minimum and maximum price in the result set as follows:

<?php
  
if ($minPrice || $maxPrice)
  {
    if (
$minPrice && $maxPrice)
    {
      
$originalPriceWhere " AND price BETWEEN '".$minPrice."' AND '".$maxPrice."' ";
    }
    elseif (
$minPrice)
    {
      
$originalPriceWhere " AND price > '".$minPrice."' ";
    }
    elseif (
$maxPrice)
    {
      
$originalPriceWhere " AND price < '".$maxPrice."' ";
    }
  }
  
$sql "SELECT MAX(price) AS maxPrice,MIN(price) AS minPrice FROM `".$config_databaseTablePrefix."products` WHERE ".$where.str_replace($originalPriceWhere,"",$priceWhere);
  
database_querySelect($sql,$result);
  
$minPrice $result[0]["minPrice"];
  
$maxPrice $result[0]["maxPrice"];
?>

Then in your sidebar HTML where you generate the slider, for the values use

<?php print $minPrice?>

and

<?php print $maxPrice?>

Cheers,
David.
--
PriceTapestry.com

Submitted by technoarenasol on Thu, 2012-10-18 13:53

technoarenasol

i want to show the min and max price of that category

i am using this code rigth now
$sql1 = "SELECT min(price) as resultCount FROM `".$config_databaseTablePrefix."products` ";
database_querySelect($sql1,$rows1);
$minprices= $rows1[0]["resultCount"];
$sql1 = "SELECT max(price) as resultCount FROM `".$config_databaseTablePrefix."products` ";
database_querySelect($sql1,$rows1);
$maxprices=$rows1[0]["resultCount"];

which show the minimum price and maximum price from the database without any condition
which shows the min and max allowed slider value
when i select any filter then it must show the minimum price and maximum price of that category

something like this
$sql1 = "SELECT max(price) as resultCount FROM `".$config_databaseTablePrefix."products` where brand='abc'";

but i am not getting how to set where condition when there is multiple filters

Submitted by support on Thu, 2012-10-18 14:24

Hi,

You can use $priceWhere to limit by filters currently applied, but since that includes any price filtering that part of the SQL needs to be removed which is what the first part of the code does. Have a go with this, based on the code from your post:

  if ($minPrice || $maxPrice)
  {
    if ($minPrice && $maxPrice)
    {
      $originalPriceWhere = " AND price BETWEEN '".$minPrice."' AND '".$maxPrice."' ";
    }
    elseif ($minPrice)
    {
      $originalPriceWhere = " AND price > '".$minPrice."' ";
    }
    elseif ($maxPrice)
    {
      $originalPriceWhere = " AND price < '".$maxPrice."' ";
    }
  }
  $sql1 = "SELECT min(price) as resultCount FROM `".$config_databaseTablePrefix."products` WHERE ".$where.str_replace($originalPriceWhere,"",$priceWhere);
  database_querySelect($sql1,$rows1);
  $minprices= $rows1[0]["resultCount"];
  $sql1 = "SELECT max(price) as resultCount FROM `".$config_databaseTablePrefix."products` WHERE ".$where.str_replace($originalPriceWhere,"",$priceWhere);
  database_querySelect($sql1,$rows1);
  $maxprices=$rows1[0]["resultCount"];

Cheers,
David.
--
PriceTapestry.com

Submitted by richard on Wed, 2016-07-13 09:45

Hi David

I wish to pre-populate the min and max prices from the filtered results in the price range search.

I have inserted the following in search.php

<?php
  if ($minPrice || $maxPrice)
  {
    if ($minPrice && $maxPrice)
    {
      $originalPriceWhere = " AND price BETWEEN '".$minPrice."' AND '".$maxPrice."' ";
    }
    elseif ($minPrice)
    {
      $originalPriceWhere = " AND price > '".$minPrice."' ";
    }
    elseif ($maxPrice)
    {
      $originalPriceWhere = " AND price < '".$maxPrice."' ";
    }
  }
  $sql = "SELECT MAX(price) AS maxPrice,MIN(price) AS minPrice FROM `".$config_databaseTablePrefix."products` ".$where.str_replace($originalPriceWhere,"",$priceWhere);
  database_querySelect($sql,$result);
  $minPrice = $result[0]["minPrice"];
  $maxPrice = $result[0]["maxPrice"];
?>

and then altered

            print "<input name='minPrice' style='display:inline;' class='pt_sf_price' type='number' value='".htmlspecialchars($minPrice,ENT_QUOTES,$config_charset)."' />";

to

            print "<input name='minPrice' style='display:inline;' class='pt_sf_price' type='number' value=$minPrice />";

As will guess, I have had no luck.

I suspect the main problem is what I have done in search.php.

I would appreciate a nudge in the right direction again please!

Many thanks

Richard

Submitted by support on Wed, 2016-07-13 10:02

Hello Richard,

I'd missed out the WHERE keyword in the query sorry, where you have:

   $sql = "SELECT MAX(price) AS maxPrice,MIN(price) AS minPrice FROM `".$config_databaseTablePrefix."products` ".$where.str_replace($originalPriceWhere,"",$priceWhere);

...REPLACE with:

   $sql = "SELECT MAX(price) AS maxPrice,MIN(price) AS minPrice FROM `".$config_databaseTablePrefix."products` WHERE ".$where.str_replace($originalPriceWhere,"",$priceWhere);

Cheers,
David.
--
PriceTapestry.com

Submitted by richard on Wed, 2016-07-13 11:06

Hi David,

Thanks, that did the trick.

if I searched with minPrice £200 it would request that I amend field to either £199.99 or £200.99.

I corrected that by using
type='number' step='any' min='0'

Best regards

Richard

Submitted by richard on Fri, 2016-07-15 15:25

Hi David

Is there anyway to ensure the that min/max price variables in the url always match those in reported in the min/max price search filter box?

Best regards,

Richard

Submitted by support on Mon, 2016-07-18 09:06

Hi Richard,

You could use an IF condition so that the min / max price boxes are only dynamically populated if _not_ present in the URL, and therefore if the user makes changes the values will always reflect what is in the URL. This also means less code involved since the price range WHERE clause in effect does not need to be constructed and removed - have a go with just:

<?php
  
if ((!$minPrice) && (!$maxPrice))
  {
    
$sql "SELECT MAX(price) AS maxPrice,MIN(price) AS minPrice FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere;
    
database_querySelect($sql,$result);
    
$minPrice $result[0]["minPrice"];
    
$maxPrice $result[0]["maxPrice"];
  }
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by richard on Mon, 2016-07-18 10:07

Hi David,

Many thanks.

I like this solution.

Best regards

Richard