Hi guys,

Help me out please. How can I setup views filters to take params from the URL?

I'm stuck, please help

Comments

yched’s picture

"filters taking params from the URL" = Views Arguments :-)

If you haven't already, you should start by digging there

mixey’s picture

I'm there, but I'm walking around and around, can't get main idea how to do that :(

how can I filter nodes by CCK field price where price is in range of 100 and 150 (those number I'll pass throw the url or with $_GET method from the search form)

I could not understand how to use Argument handling code for that. could you please help me with step by steps instructions or with screenshot?

And thanks for your time

mixey’s picture

By now I came up with the following working code:

if ($args[0] && arg(1) == "asc") {
  drupal_set_message(arg(1));
  $view->sort[0]['vid'] = 7;
  $view->sort[0]['position'] = 0;
  $view->sort[0]['field'] = 'node_data_field_price.field_price_value';
  $view->sort[0]['sortorder'] = 'ASC';
  $view->sort[0]['options'] = '';
  $view->sort[0]['tablename'] = '';
  $view->sort[0]['id'] = 'node_data_field_price.field_price_value';
}
$view->is_cacheable = 0;
return $args;

but it's just sorting...

Who knows how to make filter code work?

if ($args[0]) {
  drupal_set_message(arg(1));
  $view->filter[0]['vid'] = 7;
  $view->filter[0]['tablename'] = '';
  $view->filter[0]['field'] = 'node_data_field_price.field_price_value';
  $view->filter[0]['value'] = '105';
  $view->filter[0]['operator'] = '>';
  $view->filter[0]['options'] = '';
  $view->filter[0]['position'] = 0;
  $view->filter[0]['id'] = 'node_data_field_price.field_price_value';
}
$view->is_cacheable = 0;
return $args;

this one generates sql errors :(


* user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '> '105') AND (node.type IN ('furniture_item')) )' at line 1 query: SELECT count(node.nid) FROM node node LEFT JOIN content_type_furniture_item node_data_field_price ON node.vid = node_data_field_price.vid INNER JOIN users users ON node.uid = users.uid LEFT JOIN i18n_node i18n ON node.nid = i18n.nid WHERE (i18n.language ='ru' OR i18n.language ='' OR i18n.language IS NULL) AND ( (.field_price_value > '105') AND (node.type IN ('furniture_item')) ) in c:\program files\easyphp1-8\www\furniture\includes\database.mysql.inc on line 172.
* user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '> '105') AND (node.type IN ('furniture_item')) ) LIMIT 0, 10' query: SELECT node.nid, node.title AS node_title, node.changed AS node_changed, node_data_field_price.field_price_value AS node_data_field_price_field_price_value, users.name AS users_name, users.uid AS users_uid FROM node node LEFT JOIN content_type_furniture_item node_data_field_price ON node.vid = node_data_field_price.vid INNER JOIN users users ON node.uid = users.uid LEFT JOIN i18n_node i18n ON node.nid = i18n.nid WHERE (i18n.language ='ru' OR i18n.language ='' OR i18n.language IS NULL) AND ( (.field_price_value > '105') AND (node.type IN ('furniture_item')) ) LIMIT 0, 10 in c:\program files\easyphp1-8\www\furniture\includes\database.mysql.inc on line 172.

TKS’s picture

mixie -

OK, I *think* I understand what you're trying to do. (yched is right -- if all you wanted to do was pass along a value and find nodes where the value EQUALED 100, then a simple argument will do it. But when you want to do a range, some funky argument handling code is required. At least for now, that is -- I seem to recall a post that said other operators are in the works for Views in the near future....)

I muddled through a similar problem -- see http://drupal.org/node/150248 -- so let's see if the code below will do what you want:

// Generate the filter variables, using arguments
// passed in to the view from the URL

$lowend = $args[0];
$highend = $args[1];

// Add filter to establish low end of price range
  $view->filter[] = array(
    'vid' => $view->vid,
    'tablename' => '',
    'field' => 'node_data_field_price.field_price_value_default',
    'value' => $lowend,
    'operator' => '>',
    'options' => '',
    'position' => count($view->filter),
    'id' => ''node_data_field_price.field_price_value_default',
  );

// Add filter to establish high end of price range
  $view->filter[] = array(
    'vid' => $view->vid,
    'tablename' => '',
    'field' => 'node_data_field_price.field_price_value_default',
    'value' => $highend,
    'operator' => '<',
    'options' => '',
    'position' => count($view->filter),
    'id' => ''node_data_field_price.field_price_value_default',
  );

// IMPORTANT: Invalidate the cached query for this view, as it'll need to be regenerated on each request
$view->is_cacheable = 0;

This assumes that -- based on the example you gave -- 100 would be the first variable passed, and 150 would be the second. IE: http://www.yoursitename.com/viewpath/100/150. (It also assumes that your use of CCK isn't too different from mine, that i18n or other modules aren't adding wrinkles, and that I haven't munged this entirely!!!)

If there are other arguments before those, you'd need to change the values for $lowend and $highend to $args[2] or $args[37] or whatever.

I won't pretend to fully understand the code above -- I basically experimented with what quicksketch posted at http://drupal.org/node/70145#comment-204051 to get working code for my site, and am now just swapping your variables into what worked for me. But if it works for you, declare victory, and go back and figure out *why* it works later! :)

Please post back and let folks know if this does the trick. Good luck.

TKS

mixey’s picture

It didn't work for me :( but good news are that it doesn't generate sql errors anymore.

  $lowend = $args[0];
  $highend =$args[1];
  drupal_set_message("begin");
// Add filter to establish low end of price range
  $view->filter[] = array(
    'vid' => $view->vid,
    'tablename' => '',
    'field' => 'node_data_field_price.field_price_value_default',
    'value' => $lowend,
    'operator' => '>',
    'options' => '',
    'position' => count($view->filter),
    'id' => ''node_data_field_price.field_price_value_default',
  );

// Add filter to establish high end of price range
  $view->filter[] = array(
    'vid' => $view->vid,
    'tablename' => '',
    'field' => 'node_data_field_price.field_price_value_default',
    'value' => $highend,
    'operator' => '<',
    'options' => '',
    'position' => count($view->filter),
    'id' => ''node_data_field_price.field_price_value_default',
  );


// IMPORTANT: Invalidate the cached query for this view, as it'll need to be regenerated on each request
$view->is_cacheable = 0;

but id does nothing :(
there is drupal_set_message("begin"); for testing purposes and it doesn't work too.

I'll try to play with another drupal installation, without i18n. For the CCK field price, I'm using Decimal field type.

Any suggestions?

mixey’s picture

All the code above I'm pasting in to the Argument handling code area.
I hope it's right

mixey’s picture

with clean drupal installation plus Views and CCK, I got same result

mixey’s picture

finally got the solution http://drupal.org/node/164471

moshe weitzman’s picture

Status: Active » Fixed
Anonymous’s picture

Status: Fixed » Closed (fixed)
summit’s picture

Mikey,

Could you post your solution code?
Thanks in advance,
greetings,
Martijn

KentBye’s picture

@Martijnhaan: Mikey did post his solution & explanation here: http://drupal.org/node/164471

summit’s picture

Hi,

I am not able to follow this solution of Mikey in my situation..
I have 3 filters.
- The first is Node: Published
- The second is Node: Distinct
- The third is the Taxonomy: Terms for [my vocabulary]

The third is the one I would like to be able to use the arguments on.

I tried the following argument handling code:

$args[0] = $args(1);
$view->filter[2]['value'] = $args[1];
$view->is_cacheable = 0;
return $args;

But then I get the following errors...

warning: array_map() [function.array-map]: Argument #2 should be an array in /home/public_html/modules/views/modules/views_taxonomy.inc on line 493. 
warning: Invalid argument supplied for foreach() 

Can somebody help please to get the filter working on taxonomy terms, with depth of 3?
Thanks a lot in advance!

greetings,
Martijn

gopi2msg’s picture

Title: Assigning views filters dynamicly » where want to paste code ???
Version: 5.x-1.0 » 6.x-2.0

friends I am new to writing code in drupal, can i know where to paste this code and how want to be tested

dawehner’s picture

Title: where want to paste code ??? » Assigning views filters dynamicly
Version: 6.x-2.0 » 5.x-1.0

This is drupal 5, please keep it there.

shaina2231’s picture

Status: Closed (fixed) » Active

I need to pass dynamic CCK field values to Filters In contains Query I dont know how to do plz help

esmerel’s picture

Status: Active » Closed (fixed)

This issue has been closed; if you have a new problem, or a question on another version, open a new issue.