Posted by Roi Danton on December 3, 2008 at 12:31am
18 followers
Jump to:
| Project: | Views |
| Version: | 6.x-2.9 |
| Component: | Views Data |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (fixed) |
Issue Summary
I want to use only the result a view produces (list of the terms, nodes etc). The Object which is returned by views_get_current_view() has a nice Array with all the results of the current view. Unfortunately I haven't found a member function for the view Object (which views_get_view() returns) that creates such an array and I don't know how views_get_current_view() creates that result Array. Maybe someone could give a hint?
Comments
#1
Nevermind, I've got it with the render method (maybe there is another which needs less ressources?):
<?phpfunction views_get_view_result($viewname) {
$view = views_get_view($viewname);
$view->render();
return $view->result;
}
?>
#2
preview() would probably be better.
#3
$view->preview() would probably be better than $view-render() rather.
#4
Ah, okay, thx for the follow up! :)
#5
I have wrote a small tutorial so the module developer knows what to expect from the result array.
#6
i just edited that tutorial to add some argument handeling code as well as changing ->preview() to ->execute() ... which i hope is correct still.
#7
Works and it seems that the execute() method doesn't create HTML code like preview() (contains no information about rendertime). Though the Array size is only <1% smaller (e.g. 845496 vs 845510 Byte) its more correct that way. Thx!
EDIT:
However, the disadvantage is that execute() doesn't accept arguments as parameter. If one wants include arguments on-the-fly than preview() has to be used.EDIT #2: I've seen you added it already.#8
Automatically closed -- issue fixed for two weeks with no activity.
#9
Hi sorry I have to reopen the thread but I notice using the method mentioned here http://drupal.org/node/342132, I was able to get only the 1st 10 results. My settings for the view (flag actually) was set to 20 but its seems to ignore it? Thanks.
#10
Use $view->pre_execute() to get the pager settings properly used.
#11
Thanks for your reply.
Is there any document I can refer to with usage examples? I can't seem to find anywhere with search...still new to views 2 api..tks : )
edit: I just found out the method on the api page
For example, set items to 20 instead of default 10
$view->set_items_per_page(20);$view->execute();
$result = $view->result;
#12
So $view->execute() doesn't heed the pager settings therefore you recommended $view->preview() in #3? Does the following code heed the pager settings properly?
$view->pre_execute();$view->execute();
return $view->result;
$view->preview();return $view->result;
#13
Yes, calling pre_execute() prior to execute() works. The advnatage to using pre_execute() and then execute() is that you won't spend time rendering a view you won't display, so I would encourage using whichever method works best for you.
#14
Based on http://drupal.org/node/342132, after looking through the Views2 code, I've found that this will only work for me if I use
$view->init_display();$view->pre_execute();
$view->execute();
return $view->result;
Without
$view->init_display(), the handlers for the displays seem not to have been initialized, but the Views2 code is calling the pre_execute() on those (non-existent) handlers.Is using
$view->init_display()a bad idea?#15
init_display() is safe to use. It is a good idea.
#16
Automatically closed -- issue fixed for 2 weeks with no activity.
#17
It seems that the creation of the result array in view::execute() doesn't heed overridden display settings or displays at all. E.g. we use a display with id my_display which has different filter settings than the default display of the view. When loading the result array with
$view = views_get_view($viewname);$view->set_display('my_display');
$view->pre_execute();
$view->execute();
return $view->result;
#18
you could at least try out
<?php$view = views_get_view($viewname);
$view->set_display('my_display');
$view->pre_execute();
$view->execute('my_display');
return $view->result;
?>
#19
I'm sorry, it was my fault. The version posted in #17 works! (#18 works too, but it is enough to use init_display instead of set_display then)
I just had problems to retrieve the correct display id. Is there another way than that described here with the default views GUI (?):
I've updated the tutorial accordingly. Instead of set_display and set_arguments it'd also be possible to use
$view->pre_execute($args);$view->execute($display_id);
#20
The keys for CCK fields are assigned with a bad behavior. Always the name of the CCK field with the lowest weight (first appearance) is appended to node_data_ followed by the name of the current field.
Example with two fields field_text and field_numberfloat.
field_text has lowest weight:
[0] => stdClass Object(
[nid] => 102
[node_data_field_text_field_text_value] =>
[node_data_field_text_field_text_format] =>
[node_type] => artikel
[node_vid] => 102
[node_data_field_text_field_numberfloat_value] =>
[node_status] => 1
[node_translate] => 0
[node_created] => 1227473981
)
field_numberfloat has lowest weight:
[0] => stdClass Object(
[nid] => 102
[node_data_field_numberfloat_field_numberfloat_value] =>
[node_type] => artikel
[node_vid] => 102
[node_data_field_numberfloat_field_text_value] =>
[node_data_field_numberfloat_field_text_format] =>
[node_status] => 1
[node_translate] => 0
[node_created] => 1227473981
)
This behavior is annoying when processing the view result. Is this by design?
#21
did you looked once at the views query, it's something incredible powerful but dynamic! so the tables have to have names like this.
so at the end i think this is by design
#22
This is by design. Views automatically checks to see if any of the fields share a table, and if so, the table only gets added in once. That means the table has the first name assigned to it. The previous behavior joined in a new table in for every field, which gave you the names you are expecting to see, but made for a much less efficient query.
#23
Thanks for the explanation. Has been added to the tutorial.
#24
Hi
I need to recover the related nodes involved in a relationship, I dont know how to do it. Maybe I can make a db query to get the nodes references of other node, or working with views and accesing to the data of the view programally? I really need to solve it, I dont know how?? Please write back soon.
#25
Hi, thanx for a way from my problem, now just how to print this..
<?phpprint $view->result[node_data_field_numberfloat_field_text_value];
?>
doesn't work..
thank You, would be big helper for me
#26
I use something like the following in the php footer:
$view = views_get_current_view();foreach ($view->result as $result) {
$whatyouwant = $result->fieldyouwant;
\\where field you want would be node_data_field_numberfloat_field_text_value
print $whatyouwant;
#27
He asked the some question in another issue, and there the answer was given.
#28
$view = views_get_current_view();foreach ($view->result as $result) {
$whatyouwant = $result->fieldyouwant;
\\where field you want would be node_data_field_numberfloat_field_text_value
print $whatyouwant;
Hi i'm developing themes and views result is sometimes very complicated and unnecessary...
i need only
<ul class="something"><li> views Fields </li>
<ul>
but problem is ; Fields -> Node: Title is link to node and i'm trim this field to a maximum length (34)
how can i render that options ?
#29
Hi all.
I have 2 CCK image fields in a View on 1 Default display with "Fields" output.
Now.
When I run either:
$result = views_get_view_result($name, NULL, $args)
or:
$view = views_get_view($name);
$view->pre_execute($args);
$view->execute();
$result => $view->result;
then $result array contains only ONE, first field from my fields and I have no any mention in the result about my second image field.
When I see Live Preview in Views UI or I run $view->preview(), HTML output contains BOTH the fields.
Debugging View execution showed that view query doesn't even contain second field in SELECT list. Its added somehow later.
So the question is - how can I get result array with ALL my fields?
#30
NULL, cannot be your display.
#31
This makes no difference. Problem didn't go. Please reread more carefully what I'm asking.
#32
OK. Then you have a multiple cck imagefield. This values are stored in the handler.
If you execute your view you can find it in $view->fields['name']->items[$nid]
#33
.
#34
#35
This is actually a very interesting question and I am also interested in the answer.
While the code snippets above work, they only provide a list of IDs. How to make them go the whole way and render each field's value in an array elements?
#36
#35:
http://drupal.org/project/views_php_array might do what you want.
#37
#38
Dereine:
Thanks you that's what I looked for.
#39
Automatically closed -- issue fixed for 2 weeks with no activity.
#40
Just like OnkelTem from #29 above, I am seeing everything fine under views preview, but am not seeing any of my CCK fields (text, date, integer, etc) when I do the following:
$view = views_get_view($name);
$view->init_display();
$view->pre_execute($args);
$view->execute();
$result => $view->result;
I get the following:
Array(
[0] => stdClass Obj
(
[nid] => 111
[node_title] => My Title
)
)
Even if I call preview() instead of execute() I still do not get any of my CCK fields returned.
I'm stuck on this and open to any/all ideas. Thanks!
(using views 6.x-2.12)