For the last three days I was fighting with filters. I was trying to find a way to filter nodes by one field (CCK price) to get results where the price would be in some range.

User TKS has come up with a solution, but it didn't work for me. (http://drupal.org/node/150248) Thanks Troy for your time and help!
And then with the help of Russian drupaller Piyavkin I finally made it work.

So here we go:

I assume that you have Drupal, Views and the CCK module installed.
In my case I had the CCK Decimal field where I'm saving the prices of items. The goal was to make a small form where the user will enter a price range and/or other search parameters and then Views should display those nodes.

1) Create your custom content type (CCK).
2) Add a page view. Select provide page view, use the pager and specify all the settings you need.
3) In the Filters area, add "Node: Published Equals YES"
4) Add "Decimal: Price (field_price) is greater then or equals to 1"
5) Add "Decimal: Price (field_price) is Less than or Equal to 10000000000"

Add the following code to the Argument handling code area.

if($_GET['pr_from']) { $lowend = $_GET['pr_from']; } else { $lowend = 1; }
if($_GET['pr_to']) { $highend = $_GET['pr_to']; } else { $highend = 10000000000; }
/***************************************************************************/
$view->filter[1]['value'] = $lowend;
$view->filter[2]['value'] = $highend;

$view->is_cacheable = 0;
return $args;

Note
You may want to consider using the intval function to get the integer value.

if($_GET['pr_from']) { $lowend = intval($_GET['pr_from']); } else { $lowend = 1; }

Ensure that $view->filter[X] has the right indexes. Getting them is very easy. Think of Filters as arrays and count from the top of the filter list to the bottom (start counting with 0).
So in our case 0 is node published, 1 is "Decimal: Price (field_price) is greater then or equals to 1" and 2 is "Decimal: Price (field_price) is Less than or Equal to 10000000000".

You may notice that in the Argument handling code area there are $_GET['pr_from'] and $_GET['pr_to']. Those are GET variables that I'm passing from the form to the Views.
Form:

<form id="search_form" name="search_form" method="get" action="http://www.example.com/view_url">
    from:
    <input type="text" name="pr_from" />
    to:
    <input type="text" name="pr_to" />
    title has:
    <input type="text" name="ttxt" />
    shop id:
    <input type="text" name="shop" />
    <br />
    <br />
    <input type="submit" name="Submit" value="Search" />
    <input class="cancel" type="submit" name="close_btn" value="Cancel" />
</form>

That's the way you pass parameters from the form to the View.

But you can make it a little bit more powerful, by extending the argument handling code:

if($_GET['pr_from']) { $lowend = $_GET['pr_from']; } else { $lowend = 1; }
if($_GET['pr_to']) { $highend = $_GET['pr_to']; } else { $highend = 10000000000; }
if($_GET['ttxt']) { $ttxt = $_GET['ttxt']; } else { $ttxt = ''; }
if($_GET['shop']) { $owner_shop = array($_GET['shop']); } else { $view->filter[4]['operator'] = 'NOR'; $owner_shop = array("-10"); }
/***********************************************************************************************************************/

$view->filter[1]['value'] = $lowend;
$view->filter[2]['value'] = $highend;
$view->filter[3]['value'] = $ttxt;
$view->filter[4]['value'] = $owner_shop;

$view->is_cacheable = 0;
return $args;

In the Filters area add two more filters:
1) Node: Title Contains Any Word
2) Node: Author Name is one of (choose whatever you like we will overwrite this in the argument handling code area)

And again don't forget to check $view->filter[X] indexes

If the user enters something in Title field of the search form, Views will look for the nodes which have that word in the title and the price is in the range of xx to zzz.
Also if the user enters an Author ID then Views will filter by this author. If not, Views will show all authors.

I hope my explanation is clear to you guys and this will be helpful for someone.
Cheers,
mixey

Comments

rusdvl’s picture

subscribing

czuroski’s picture

subscribing

abhishekbhaduri’s picture

Hello,
I need something similar but I am on drupal 6.x and I had questions on how does your process translate for 6.x. Also, where do these pieces of code go? Do i create a 'page' type content and put the form code in the body?

thanks,
Abhi.

sisko’s picture

I also am looking for a Drupal 6 compatible work around

jimkomara’s picture

I am struggling with the jquery sliders, any way to set a price range would be good but I keep running into road blocks.

prabeen.giri’s picture

where is the argument handling code area? where is the right place to write the argument handling code??
Lets say I have range Price: 2000 to 10000. which comes from url.
Now i want to write the argument handling code to tell the views filter that my initial range is 2000 and other range is 10000.

nikitas’s picture

subscribing!