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

Roi Danton’s picture

Nevermind, I've got it with the render method (maybe there is another which needs less ressources?):

function views_get_view_result($viewname) {
  $view = views_get_view($viewname);
  $view->render();
  return $view->result;
}
merlinofchaos’s picture

Status: Active » Fixed

preview() would probably be better.

merlinofchaos’s picture

$view->preview() would probably be better than $view-render() rather.

Roi Danton’s picture

Ah, okay, thx for the follow up! :)

Roi Danton’s picture

I have wrote a small tutorial so the module developer knows what to expect from the result array.

a_c_m’s picture

i just edited that tutorial to add some argument handeling code as well as changing ->preview() to ->execute() ... which i hope is correct still.

Roi Danton’s picture

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.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

dropchew’s picture

Version: 6.x-2.1 » 6.x-2.2
Status: Closed (fixed) » Active

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.

merlinofchaos’s picture

Use $view->pre_execute() to get the pager settings properly used.

dropchew’s picture

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;
Roi Danton’s picture

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;

If yes maybe it's better to use the preview() method though?

    $view->preview();
    return $view->result;
merlinofchaos’s picture

Status: Active » Fixed

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.

mmilo’s picture

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?

merlinofchaos’s picture

init_display() is safe to use. It is a good idea.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Roi Danton’s picture

Status: Closed (fixed) » Active

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;

this array still contains the items of the default display. I'm continuing looking for a solution but maybe someone can give a helpful hint.

dawehner’s picture

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;
?>
Roi Danton’s picture

Status: Active » Fixed

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 (?):

How to find a display id

On the edit page for the view in question, you'll find a list of displays at the left side of the control area. "Defaults" will be at the top of that list. Hover your cursor over the name of the display you want to use. An URL will appear in the status bar of your browser. This is usually at the bottom of the window, in the chrome. Everything after #views-tab- is the display ID, e.g. page_1.

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);
Roi Danton’s picture

Version: 6.x-2.2 » 6.x-2.5
Status: Fixed » Active

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?

dawehner’s picture

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

karens’s picture

Status: Active » Closed (works as designed)

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.

Roi Danton’s picture

Thanks for the explanation. Has been added to the tutorial.

indiralanza’s picture

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.

czeky’s picture

Hi, thanx for a way from my problem, now just how to print this..

print $view->result[node_data_field_numberfloat_field_text_value];

doesn't work..

thank You, would be big helper for me

Chad_Dupuis’s picture

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;
dawehner’s picture

He asked the some question in another issue, and there the answer was given.

alisamar’s picture

$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 ?

OnkelTem’s picture

Version: 6.x-2.5 » 6.x-2.9
Status: Closed (works as designed) » Active

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?

dawehner’s picture

Status: Active » Fixed

NULL, cannot be your display.

OnkelTem’s picture

Status: Fixed » Active

This makes no difference. Problem didn't go. Please reread more carefully what I'm asking.

dawehner’s picture

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]

dawehner’s picture

Status: Active » Postponed (maintainer needs more info)

.

esmerel’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)
Gabriel R.’s picture

Status: Closed (cannot reproduce) » Active

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?

dawehner’s picture

#35:

http://drupal.org/project/views_php_array might do what you want.

esmerel’s picture

Status: Active » Fixed
Dood59’s picture

Dereine:

Thanks you that's what I looked for.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

incaic’s picture

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)

S.H.Wang’s picture

Issue summary: View changes

In my individual view template ( I mean in my file: views-view--ViewName--page.tpl.php),

$view = views_get_current_view();
$vies->render(); //or $view->preview();
var_dump($v->result);

I got:

Fatal error: Maximum function nesting level of '100' reached

Only:

$view = views_get_current_view();
$vies->execute();
var_dump($v->result);

works correctly.