Hide links to empty views
BWPanda - June 23, 2009 - 06:49
| Project: | Views |
| Version: | 6.x-2.6 |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
Description
I'm trying to get the results of a view using PHP, but the result of views_get_view always seems to be empty, even though there are nodes in that view...
The code I'm using is as follows:
<?php
$my_view = views_get_view('Activities_Accommodation');
$my_view->set_display('page_1');
$my_view->pre_execute($fields['title']->raw);
$my_view->execute();
dsm($my_view->result);
?>When I go to the view in question with the argument in the URL, I see two nodes; however the output of dsm() is an empty array. What am I doing wrong?

#1
Still having issues with this...
To explain what I'm wanting:
I have 2 content types: Activities and Areas. One of the fields in an Activity is a node_reference to an Area (i.e. activities are located in a specific area). I then have 2 views to display the nodes from each content type. The Areas view displays the area nodes whose titles I've set to link to the Activities view.
The problem occurs, however, when there aren't any Activity nodes in a given Area - clicking on the Area title takes you to an empty view. This is not desirable, so I'm trying to make it so that only Areas that have associated Activity nodes are displayed.
I'm now trying to use the following code, but it's still producing empty results:
<?php$my_view = views_get_view('Activities_Accommodation');
$my_view->set_arguments(array($fields['title']->raw));
$my_view->execute('page_1');
dsm($my_view->result);
?>
If anyone could help me out, or tell me of a better/easier way to do what I'm wanting, it'd be much appreciated!
#2
did you forgot pre_execute() ? its definitive needed!
#3
This code produced the following error:
<?php$my_view = views_get_view('Activities');
$my_view->pre_execute(array($fields['title']->raw));
$my_view->execute('page_1');
dsm($my_view->result);
?>
But adding
$my_view->set_display();just before the pre_execute() did the trick.Thanks dereine!
#4
Automatically closed -- issue fixed for 2 weeks with no activity.