View filter problems
splash112 - January 6, 2009 - 10:56
Working on this piece of code for some hours now... Just don't know what I do wrong.
I have a view and that view should be filtered with 2 timestamps (Time < Created < Time)... Now I only get an empty space, and when I leave the filters out my "block'.
Any suggestions?
<?php
$timezone = variable_get('date_default_timezone', 0);
$start_time = time() - (86400 * 14);
$end_time = time();
//load the view by name
$view = views_get_view('simplenews_digest');
views_view_add_filter($view, 'node', 'created', '>', $start_time, '');
views_view_add_filter($view, 'node', 'created', '<', $end_time, '');
// load this before Sanitize.
views_load_cache();
//Sanitize it (eg, create field id's, etc)
views_sanitize_view($view);
print $start_time .'<br>';
print $end_time .'<br>';
print views_build_view('block', $view);
?>
Small addition. I saw some
Small addition.
I saw some other post somewhere having a different solution to this problem. Sadly, I really need those two timestamps, because they will later be replaced by other timestamps from the simplenews digest module.
I'm not sure if created
I'm not sure if created takes a unix timestamp as a value, it takes a string (i.e. date iso or + 1 day). I'm not sure where views_view_add_filter passes its arguments along to, but you probably want to investigate that as well.
From views/handlers/views_handler_filter_date.inc:
142 $this->query->add_where($this->options['group'], "$field >= %s", $a);Thanks! Created is a unix
Thanks!
Created is a unix timestamp, I checked already 3 times ;-)
But thanks, I started playing again with the code and made a mistake, which gave me a nice look on the query.
What do you think about this:
<?php
$view = views_get_view('simplenews_digest');
views_view_add_filter($view, 'node', 'created', '>', $newsletter['start_time'], '');
views_view_add_filter($view, 'node', 'node.created', '<', $newsletter['end_time'], '');
//output the view
views_sanitize_view($view);
$newsletter['title'] = t('Title of newsletter');
$newsletter['body'] = views_build_view('block', $view);
$newsletter['teaser'] = node_teaser($newsletter['body']);
?>
The "'node', 'node.created'" part is obviously wrong.
Then the error:
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 '< '1230796799') ORDER BY node_created_created DESC' at line 1 query: SELECT node.nid, node.created AS node_created_created, node.title AS node_title, node.changed AS node_changed FROM node node LEFT JOIN uc_products uc_products ON node.vid = uc_products.vid WHERE (node.status = '1') AND (node.type IN ('product')) AND (uc_products.sell_price < '12') AND (node.created > + 0) AND (.node < '1230796799') ORDER BY node_created_created DESC in /home/tit.nl/public_html/includes/database.mysql.inc on line 174.
You see, in the wrong part of the code the timestamp is handled correctly, in the right part it's not. I made this error because I found the "table" column empty and "table.field" in the field column. Might be a problem with the empty option field? So I tried and made:
<?php
$view = views_get_view('simplenews_digest');
views_view_add_filter($view, 'node', 'created', '>', $newsletter['start_time'], '-1000000');
views_view_add_filter($view, '', 'node.created', '<', $newsletter['end_time'], '+0');
//output the view
views_sanitize_view($view);
$newsletter['title'] = t('Title of newsletter');
$newsletter['body'] = views_build_view('block', $view);
$newsletter['teaser'] = node_teaser($newsletter['body']);
?>
And got the next intentional error:
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 '< '1230796799') ORDER BY node_created_created DESC' at line 1 query: SELECT node.nid, node.created AS node_created_created, node.title AS node_title, node.changed AS node_changed FROM node node LEFT JOIN uc_products uc_products ON node.vid = uc_products.vid WHERE (node.status = '1') AND (node.type IN ('product')) AND (uc_products.sell_price < '12') AND (node.created > + -1000000) AND (.node < '1230796799') ORDER BY node_created_created DESC in /home/tit.nl/public_html/includes/database.mysql.inc on line 174.
So, I don't know. Bug in Views?
edit: might be nice to add that when I input a bug in both filters, both return a nice and valid timestamp.
Found it Had to do with:
Found it
Had to do with: views_handler_filter_timestamp
Which basically didn't handle timestamps. Patch committed and more here:
http://drupal.org/node/355420