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
)
)....
Comments
Comment #1
dawehnerYeah views_get_page_view returns not the view, it returns the page display, so you should use.
Comment #2
hellomobe commentedYou are awesome! Thank you.
Comment #3
khan2ims commentedWhat 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!
Comment #4
dawehnerHave a look at the result image with the devel module + dsm()
Comment #5
merlinofchaos commentedYou should always, if possible, refer to data via the field aliases assigned on fields.
Try
$view->style_plugin->rendered_fieldsor try$view->result->{$view->field[$id]->field_alias}Comment #6
khan2ims commentedprint $display->result{$view->field[0]->host_url};is not working either.Comment #7
dawehnerPlease take "field_alias" literally. Don't change it.
Comment #8
merlinofchaos commented0 is not a field id, either, so $view->field[0] will not work.
Comment #9
khan2ims commentedHi,
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 :(
Comment #10
dawehnerPlease post your full php code and your full views export.
This is possible to solve if you give both informations.
Comment #11
khan2ims commentedHi,
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
Comment #12
merlinofchaos commentedI 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:
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.
Comment #13
khan2ims commentedWhy
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.
Comment #14
merlinofchaos commentedThe $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_aliasprovides 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.Comment #15
khan2ims commentedIf I use
$view->field[$id]->field_aliasas 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.
Comment #16
dawehnerThe problem is not that you are naive in views3, you should perhaps learn a bit more php.
Anyway
This is probably not the best php code, but it shows you each logical step.
Comment #17
khan2ims commentedThanks a lot! I finally got it working.