You are here:  » Side Filter - Exact Matches Only


Side Filter - Exact Matches Only

Submitted by ChrisNBC on Thu, 2017-11-02 12:36 in

Hi David,

Quite a while ago you helped me change the sidebar filters on my site to tick boxes. I just noticed something odd on one of the filters which contains numbers that when for example a 5 is selected the 5.5 is picked up. I guess what is happening is a wildcard in the search. I wondered if you might possibly be able to suggest a change to the filter so that only exact matches are displayed?

Thanks in advance.

Best regards
Chris

Submitted by support on Thu, 2017-11-02 12:43

Hi Chris,

This can be done by using REGEXP and word boundary markers in place of LIKE / wildcards. I'm assuming you currently have something similar to the following code in search.php for a custom sizeFilter:

  if ($sizeFilter)
  {
    $priceWhere .= " AND size LIKE '%".database_safe($brandFilter)."%' ";
  }

The equivalent with REGEXP would be:

  if ($sizeFilter)
  {
    $priceWhere .= " AND size REGEXP '[[:<:]]".database_safe($brandFilter)."[[:>:]]' ";
  }

...and that will ensure whole word match only.

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Thu, 2017-11-02 14:34

Hi David,

Thanks for your quick response. I searched for the code above and the closest match I could find in search.php was:

{code saved}

I changed the line below:

$wheres[] = " size LIKE '%".database_safe($v)."%' ";

to a version using regex but the editor flagged an error in my code. Please could you confirm this looks like the correct section of code.

Thanks in advance.

Best regards
Chris

Submitted by support on Thu, 2017-11-02 14:56

Hi Chris,

The line;

      $wheres[] = " size LIKE '%".database_safe($v)."%' ";

...would convert to:

      $wheres[] = " size REGEXP '[[:<:]]".database_safe($v)."[[:>:]]' ";

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Thu, 2017-11-02 21:51

Hi David,

Thanks for the above. I’ve replaced the code as suggested but the issue remains. If 5 is selected 5.5, 2.5 etc are returned also if .5 is selected 5.5, 2.5 etc are returned in the results. I’m wondering if the regex boundary is identifying the .[point] correctly...

Would be grateful for any suggestions you might have on how I might be able to resolve this.

Thanks in advance.

Bets regards
Chris

Submitted by support on Fri, 2017-11-03 09:16

Hello Chris,

Ah - the decimal is considered a word boundary of course; sorry about that! An alternative then is to use LIKE with wildcards but concatenate the value between commas to cover first and last - have a go with;

      $wheres[] = " CONCAT(',',size,',') LIKE '%,".database_safe($v).",%' ";

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2017-11-03 11:19

Hi David,

Thanks for the above. The revised code seems to have resolved the issue.

Best regards
Chris