I would like to retrieve the values of a View in a custom module to set meta tags. I am having trouble getting the values of the View. To test for values, I'm working in page.tpl. When I use print_r($view), I can see the array; but when I try to print $view->result[0]->nid there is no value. I've read post after post, but I'm not getting something. Thank you in advance for guidance.

$view = views_get_page_view();
print $view->result[0]->nid;

For reference, first few lines of print_r($view)

views_plugin_display_page Object
(
    [view] => view Object
        (
            [db_table] => views_view
            [base_table] => node
            [editing] => 
            [args] => Array
                (
                    [0] => name-of-product
                )

            [use_ajax] => 
            [result] => Array
                (
                    [0] => stdClass Object
                        (
                            [node_type] => product
                            [nid] => 933
                        )

                )....
CommentFileSizeAuthor
#11 tournament-views.txt50.91 KBkhan2ims

Comments

dawehner’s picture

Status: Active » Fixed

Yeah views_get_page_view returns not the view, it returns the page display, so you should use.


$display = views_get_page_view();
print $display->view->result[0]->nid;
hellomobe’s picture

You are awesome! Thank you.

khan2ims’s picture

What to use if i want to print a field? I am trying this
print $display->view->result[0]->node_data_field_host_url_field_host_url_value;
but it's not working.
Thanks!

dawehner’s picture

Have a look at the result image with the devel module + dsm()

merlinofchaos’s picture

You should always, if possible, refer to data via the field aliases assigned on fields.

Try $view->style_plugin->rendered_fields or try $view->result->{$view->field[$id]->field_alias}

khan2ims’s picture

print $display->result{$view->field[0]->host_url}; is not working either.

dawehner’s picture

Please take "field_alias" literally. Don't change it.

merlinofchaos’s picture

0 is not a field id, either, so $view->field[0] will not work.

khan2ims’s picture

Hi,

I tried these two, but not working
print $display->result->{$display->field[$field_host_url]->field_alias}
print $display->result->{$display->field[field_host_url]->field_alias}

In theme information it shows field id as field_host_url
Not sure what should be the correct code :(

dawehner’s picture

Please post your full php code and your full views export.

This is possible to solve if you give both informations.

khan2ims’s picture

StatusFileSize
new50.91 KB

Hi,

I have attached views export. The php code is in page view.
Below is the php code. You can see I am tryiing lot's of options

<p align="right"><a href="/past-tournaments">Past Tournaments</a></p>
<?php
$display = views_get_page_view();
print $display->view->result[0]->nid;
print $display->result->{$display->field[$field_host_url]->field_alias};
print $display->result->{$display->field[field_host_url]->field_alias};
print $display->result->{$display->field[host_url]->field_alias};
print $display->result->{$display->field[$host_url]->field_alias};
print $display->result->{$display->field[host]->field_alias};
print $display->result->{$display->field[$host]->field_alias};
print $display->result->{$display->field[$field_host]->field_alias};
print $display->result->{$display->field[field_host]->field_alias};
?>
merlinofchaos’s picture

I guess you can't actually read the code you write. I provided code that gives an error, but a little thought about what was going on should've solved this for you.

You know this works:

$view->result[0]->nid;

So what did I leave out of here?

$view->result->{$view->field[$id]->field_alias}

If it helps, the {} notation is a PHP specific trickiness. YOu can read everything between the {} as a single string -- or at least, to be valid in PHP it needs to resolve to that -- and it can be used to dereference variables. If it makes it more readable:

$name = $view->field[$id]->field_alias
$value = $view->result->$name;

Do you see what's missing there?

Part of programming is understanding the code you're working with, and if you don't understand the code, you should take the opportunity to learn, because it will serve you well later on.

khan2ims’s picture

Why
print $view->view->result[0]->title;
won't work?

EDIT: Sorry I had to use print $view->view->result[0]->node_title;

But i am totally lost in displaying cck field.

merlinofchaos’s picture

The $result object contains the actual results from the query.

Views carefully aliases the results of the query to prevent namespace collisions. That means the aliases aren't friendly like 'title', they're for machines, so it can be sure to provide accurate results. 'nid' ends up being a special case because it's the primary key of the row.

So $view->field[$id]->field_alias provides the machine generated alias that's used for the field. You MUST use that, because you cannot rely on the aliases being stable. Changing conditions (reordering the fields, for example) can cause the aliases to change.

khan2ims’s picture

If I use $view->field[$id]->field_alias as it is, how do I determine which field output it should display. As you see I have tried a lot of options before,neither of which are working.

Sorry I am naive in Views3 and getting hold of it.

dawehner’s picture

The problem is not that you are naive in views3, you should perhaps learn a bit more php.

Anyway

$display = views_get_page_view();
$view = $display->view;
$result = $view->result;
$id = 'field_draw_sheet_fid';
$field = $view->field[$id];
$alias = $field->field_alias;
$item = $result[0];
return $item->{$alias};

This is probably not the best php code, but it shows you each logical step.

khan2ims’s picture

Thanks a lot! I finally got it working.

Status: Fixed » Closed (fixed)

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