These 2 patches are for views.module and views_query.inc. They will add a set of new range operators which solve the current limitation of exposed filters not being able to do range filtering - as the same field cannot be exposed twice (or more).

The 4 new operators are :

~ : within range (excluding the boundary values).
~= : within range (including the boundary values).
!~ : outside the range (excluding the boundary values).
!~= : outside the range (including the boundary values).

All values are specified like A~B with A being the lower value.

Considerations:

1. I excluded the other 4 permutations of inclusive/exclusive boundary pairs for brevity and I think those might be much less used.

2. Choice of delimiter : a normal '-' could cause problems with negative numbers, a '..' could cause problems with decimal points. Hence I adopted the '~'.

3. !~= is not logically !(~=). Think of it as (!~)=. Or just visualize the = signs being added to the > , < operators.

4. This is a quick fix for a range capability to be available via exposed filters. Like the old limitation, it does not allow multiple ranges via exposing the same field twice or more. This means that discontiguous ranges (other than inverted ranges) are still not achievable.

5. I chose to amend views.module:views_handler_operator_gtlt() rather than create a new handler like views_handler_operator_range(). This minimizes code change especially other modules'/dependants' code changes.

Please try this out and provide feedback.

Thanks.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

freeman-1’s picture

The 2nd patch file, for views_query.inc.

merlinofchaos’s picture

This is very interesting. I'm currently overwhelmed so not looking at this Right Now, but posting a note to make sure I come back to this. Ranges have been an issue for me for a long time.

merlinofchaos’s picture

Status: Needs review » Needs work

This patch contains the following problems:

1) The second patch is a reverse patch (it's removing your changes).
2) You should be able to put both files into one patch. Ideally you check out from cvs and use 'cvs diff -up' to create your patch.
3) How the ranges are used are not documented anywhere; this will confuse users to NO end. A solution to this must be found.
4) There is no need to pass the hardcoded operators in via %s; just put them in directly and remove them as arguments. (i.e, the <, >, <= and >=).

rweait’s picture

@freeman

This is wonderful. Thank you for the patch. I hope that this function is adopted in the views module.

rweait’s picture

On reflection, a couple of suggestions to make this easier for users.

First, the other filter operators (=, <=, >=, !=) are not required in the filter box. Must we* include the operator with these new range operators? Could we not use whitespace to separate the two operands?

Next, if the operators must be placed between the range operands, could the operands be added as part of the text description. e.g. "within range" becomes "within range ~"

And also, if the operands must be included between operands could we not also require the operands for the earlier filter operators and eliminate the select box?

* Yes, for now we must. For the patch requires it.

gemini’s picture

So, does that patch actually works? I installed both of them and got MySQL syntax error:

check the manual that corresponds to your MySQL server version for the right syntax to use near '~= '300000')

Also, I'm not completely sure if this will satisfy my needs when it's working - I need exposed price range filter where the min and max price limitations will be selected by user. Will this be possible with this patch?

Thanks!

freeman-1’s picture

@gemini: please bear with me, I'll put up the proper patch in several day's time. I've deleted a lot of my old test data, and will only be testing my changes against the latest stable / HEAD version then after I get the data set up again. Before then, if you can, interpret my patches and change the modules manually - I think applying the patches via some CVS apps won't work for now.

@merlinofchaos: noted all your comments, and I'll put up the proper patches soon.

@rweait: thanks for testing and the comments. I'll look into that.

Again, bear with me, while I stage my test site data again...

gemini’s picture

I noticed that CVS apps wouldn't apply the patches, so I did it manually. I'll wait and see what you come up with. Oh, btw, there is a new post on the range filters - I haven't tried the technique yet, but looks quite promising http://drupal.org/node/164471

mlncn’s picture

Subscribing! ~ ben, Agaric Design Collective

wuf31’s picture

subscribing

knight42’s picture

FileSize
165.23 KB

Hi
This is a great patch, a much needed addition. I seem to have a problem though, and am wondering if someone can help me. I installed both the patches, and all works fine until you go into views and search. Rather than the serach results its throwing up an error which I dont understand. Am using Drupal 5, everything else (including other views) work just fine, so im guessing this is breaking something. Have attached a screenshot of the error on the results page.

Also, when Im searching for a value range im putting in "100-300" in the input box. This is the right format isnt it? Again this can be seen in the pic. Thanks!

amitaibu’s picture

subscribe

freeman-1’s picture

Version: 5.x-1.6-beta5 » 5.x-1.6
Status: Needs work » Needs review
FileSize
2.84 KB

Ok, I've finally gotten round to re-staging my test site/data to test the modified patches. My basic tests indicate things are working though I've only tested for the ~= operator.

It's now diff'ed against the latest stable 5.x-1.6 version.

Please try this out and let me know how it goes.

@Merlin: Other than code comments, I'm not sure about where best to document the usage. I presume you were talking about the Views administrator UI ? I looked at those that have some usage explanation (like node, taxonomy) but they're included in the modules subdirectory under their own files. There's none for the equality handler on which I've piggybacked these additional range operators.

One way is to embed the signs themselves in the labels eg. [Withing range (inclusive) - '~='].

Stefan Vaduva’s picture

I hope this patch will be applied soon. Until then, I will manually apply it into my drupal.

Stefan Vaduva’s picture

Stefan Vaduva’s picture

I think that it will be useful to put a test in view_query.inc for the case in witch the user doesn't input a text in the form "value1 delimiter value2" and he input only one value... Something like this

if($value_range[0] == $filter['value']) //the explode function didn't explode anything
{
$query->add_where("%s.%s %s '%s'", $query->use_alias_prefix . $table, $field, $filter['operator'], $filter['value']); //old behavior
}
else
{
... //the switch statement from the patch
}

[edit] Sorry for the previous post (a problem in my browser)... Maybe someone can delete that. Thanks!

Stefan Vaduva’s picture

It seems that the range doesn't work for dates. Does anybody has the same behavior?

freeman-1’s picture

In the case where the user does not input the format of "value1 delimiter value2" but chooses a range operator, the results are varied, and in any case should not return any meaningful results since an input (semantic) error was committed.

If he intended to use (and correctly selected) the existing operators like >, < etc. than the choice is actually handled in the default (last part) case of the switch statement.

As for the date problem, my test data is not set up for dates, so others can comment. Keep in mind, that I've made very minimal change to the original handling of operands, so if it had worked for the >, < etc. operators, it should, in theory, work for this range hack too.

keptin-1’s picture

First of all thanks for providing this "hack" its what I needed on my site.

This is a hack to a hack but if you want to be able to allow the users to enter 30 to 40 (ie assuming is a date) then you can add this line of code (after you apply the patch):

$value_range = explode('~', $filter['value']); // lower-bound = [0], upper-bound = [1].

right before: $filter['value'] = str_replace(array("to"," to "),"~",$filter['value']);

Warning, I would only recommend doing this if you are going to be using this type of filter on numbers and not on text. Reason being it will replace "to" in any word, not just the one use as a delimiter. I am going to do it this way on my site because I know that the use of the ~ will be difficult for my users!

This is not ideal but hopefully a better solution can be incorporated in the next version of views that will just display two entry boxes.

Thanks and good luck!
Scott

GiorgosK’s picture

I assume this patch is not yet in views
subscribing

zeezhao’s picture

I applied views_7.patch to views-5.x-1.6.tar.gz but still could not see the "Within Range" in the dropdown list when adding a view filter... I also cleared the views cache...

I then also tried searches with format: A ~= B as suggested above for inclusive, but no results.

Please is there any other patch that needs to be applied, or is there something else I need to do? Thanks.

zeezhao’s picture

Sorry, I was using it with the "Product:Sell Price" from ubercart... So looks like I need to make some more changes to make it work for this.

asak’s picture

AMAZING! thank you!

asak’s picture

And a quick question:

How can I switch the first and second parameters?

What I mean is this: I'm trying to make a filter search a field with values such as "-0.4" , "-0.35", etc...
These represent a percentage discount (i.e. "-40%", "-35%") which is calculated using computed fields to display "-0.4" as "-40%" in the search results (yes, user searches one field but results are actually displayed from another field).

When searching, say, size, it's logical to search "from size 1 to size 3" - from small to big - and so this patch works great!
When searching a percentage discount, it makes sense to search "from -35% to -40%".
In such a case, a user would input: "-0.35 ~ -0.4" - and will get no results, since the correct syntex should be "-0.4 ~ -0.35".

Hope this makes sense...

Thanks!

asak’s picture

I asked this in the Apache Solr queue, but since range filter is working beautifully and not yet implemented - could it be possible to add a second field, where you can specify a +/- 'variance'?

This could be most usefull for searching decimal fields in combination with the range filter. That's actually what i need it for. I need users to be able to search "2.2cm - 5.4cm +/- 0.2cm" or even better "2.2cm - 5.4cm +/- 10%" (!!!).

This could serve VERY well on e-commerce sites. Users could search for "Price: 100$-200$ with a 10% variance" without needing to calculate what that actually means.

Most importantly, results could automatically be filttred with a 10% variance, and ensure that users are exposed to the broadest range of product while staying within (actually just a little bit out..) thier search criteria. Nifty marketing.

nishitdas’s picture

subscribing

I would like to see a similar patch for 6.x

Bricks and Clicks Marketing’s picture

Hi all,

I'm a little unclear on patching this - are two patches still required? One for the ...module and one for ...query.inc?

And it seems to need views2.module? I don't see that anywhere in module.

Thanks!

Arp

Bricks and Clicks Marketing’s picture

OK - I figured out how to patch, but I'm still only seeing equal/less than/great than as an option. Should there be more options, or am I doing something wrong?

kreativalab’s picture

how i can use this patch if i want search my user by age,...with the birthday and computed field doesn't work...

asak’s picture

This patch works with decimal values. it doesn't for computed fields, or other non-decimal fields.

I really like this patch, but i'm finding users are having a hard time understanding how to use it.
What i would like, is to separate the range into 2 box - one for min. and one for max.
That would be way more intuitive.

Anyone know how this can be done?

I'm using custom forms to build the exposed filter, so i think this shoulden't be too hard, but i don't know how to take 2 values and make them one.

Thanks.

Bricks and Clicks Marketing’s picture

I really don't understand it either, but it's vital for any ecommerce shop that wants to allow people to search for price ranges.

czeky’s picture

any luck with D6 implementation?

czeky’s picture

it was working for me in D5, I was using "Within a range (inclusive)" for computed field (it was an age calculation), now I'm looking how to apply this to Views2 and D6

asak’s picture

@czeky: Could you share with us how did you get that operator ("Within a range (inclusive)") to be available to computed fields in D5?

thanks!

czeky’s picture

well, I didn't have problems, so I cant tell You about some workaround.. manually patched using patch 7 both files.. and the option was there...

I had CCK field "birth" where user entered year, fg. 1975, and I wanted to calculate age, and I think using computed field was only option..

created computed field in node profile content type, set this to "raw input", data type "int", lenght "4", default value "0" and applied basic age calculation php snippet

it showed in views immediatelly, added to fields, in filters I set "Within range (inclusive)" value "0-99" - this will be basic option for users, age between 0-99 years, exposed that filter, labeled "Age from-to" filter setting optional on, default on, force single off, lock operator on, provided page view and it worked..

asak’s picture

I could swear that didn't work.

I'm still a bit shocked this really works...

Thank you!

czeky’s picture

I have this instalation online, even I moved to D6 in the beginning, so its ugly, its just for a testing purposes only, but You can see it here

http://czeky.com/ekoseznamka/

its on the top of the page, labeled "Věk od-do" and in the input field is "0-99", and working, if You put "28-30" in the search field, it will list you all user in that age..

BTW: does someone know how to load CCK field from user-profile.tpl.php, or user-picture.tpl.php?

I have a field "gender", and want to change user picture according to Male/Female.. condition would be simple of course, but I can't get CCK field_gender content-profile node loaded into user-picture/user-profile...

pan0s’s picture

amazing job....well done...but user friendliness as far as the usability of the exposed filters is concerned is a bit on the down side. This can be completely fixed if (range) values are entered in their own textfields....any thoughts about that...?

many kudos for the patch

emdalton’s picture

Date Range allows the two dates to be entered into two different fields. Maybe it would be worth looking to see how they implemented that.

askibinski’s picture

esmerel’s picture

Status: Needs review » Closed (won't fix)

At this time, only security fixes will be made to the 5.x version of Views.