Last updated April 18, 2011. Created by Roi Danton on December 3, 2008.
Edited by jhodgdon, lisarex, a_c_m. Log in to edit this page.
This function has been inserted into to the Views module since v2.8.
#495606: Programming addition to Views help
Every view creates an array which contains the result of the query. Each view item has its own object inside this array. This is very handy if you want to use a view result without theming e.g. for further processing like used in Linodef (upon RC3 release).
The corresponding issue is #341880: How to get a "result Array" with views_get_view() (as with views_get_current_view()).
Investigate the result of a view
Here is a function which returns the result of a view by name. It is possible to use arguments and set a certain view display.
<?php
/**
* Get the result of a view.
*
* @param string $name
* The name of the view to retrieve the data from.
* @param string $display_id
* The 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.
* @param ...
* Any additional parameters will be passed as arguments.
* @return
* array
* An array containing an object for each view item.
*/
function views_get_view_result($name, $display_id = NULL) {
$args = func_get_args();
array_shift($args); // remove $name
if (count($args)) {
array_shift($args); // remove $display_id
}
$view = views_get_view($name);
if (is_object($view)) {
if (is_array($args)) {
$view->set_arguments($args);
}
if (is_string($display_id)) {
$view->set_display($display_id);
}
else {
$view->init_display();
}
$view->pre_execute();
$view->execute();
return $view->result;
}
else {
return array();
}
}
?>Get the result for the current view:
<?php
function views_get_current_view_result() {
$view = views_get_current_view();
return $view->result;
}
?>Heed that only the view items for the first page are returned. If the result array should contain all items then set the numbers of items per page to 0 (no limit).
Structure of the result array
The Array contains an object for each view item. Imagine a site has only one node then a node view type without fields and sort criteria would look like this:
<?php
Array
(
[0] => stdClass Object
(
[nid] => 1
)
)
?>So by default the view item object only contains the id of the view type:
- nid
- Node view type
- vid
- Node version view type
- tid
- Term view type
- uid
- User view type
- fid
- File view type
- cid
- Comment view type
Additional the content of some view object types is added if they are defined. Those object types are:
- Fields
- Sort criteria
Example
Here we have a term view type with the following content:
Fields
- Taxonomy: Vocabulary name
- Taxonomy: Term
Sort criteria
- Taxonomy: Weight
So the obtained result array looks like this (only one object as example given here):
<?php
Array
(
[0] => stdClass Object
(
[tid] => 3
[vocabulary_name] => Planets of Sol
[term_data_name] => Earth
[term_data_vid] => 1
[term_data_weight] => 0
)
)
?>CCK Fields
When using CCK fields the name pattern is as following:node_data_%first_field_name_%current_field_name_%value
where:
- %first_field_name
- The name of the CCK field with the lowest weight (first appearance).
- %current_field_name
- The name of the CCK field the value belongs to.
- %value
- Can be value, format etc (depends on the current field).
Comments
How to call this values
Hi,
how can I call this results - perhaps in a view header?
..print $view -> result[2]['nid'];.
.. this won't work
You can put this function in
You can put this function in a module or template.php for example.
I call it in my page.tpl.php like this:
$view_result = views_get_view_result($view_name);foreach($view_result as $item){
$node = node_load($item->nid);
print '<h3 id="'.$node->nid.'">'.$node->title.'</h3>';
print $node->body;
}
in this example i am selecting node id's in my view. So if your view selects something else, then you have handle accordingly.
thanx
I'm getting correct array output, but how to print it? (in View header)
<?phpprint $view->node_data_field_dim_big_d_field_dim_big_d_value[0]['value'];
?>
doesn't seem to work
<?php
Array
(
[0] => stdClass Object
(
[nid] => 123457832
[node_title] => 6024
[node_language] => cs
[node_data_field_dim_d_field_dim_d_value] =>
[node_type] => single_row_ball_bearings
[node_vid] => 123457832
[node_data_field_dim_big_d_field_dim_big_d_value] => 180.000
[node_data_field_dim_big_b_field_dim_big_b_value] => 28.000
[node_data_field_blr_dynamic_cr_field_blr_dynamic_cr_value] => 85.000
[node_data_field_blr_stytic_cor_field_blr_stytic_cor_value] => 79.400
[node_data_field_lsfl_grease_field_lsfl_grease_value] => 3300.000
[node_data_field_lsfl_grease_field_lsfl_oil_value] => 4000.000
)
)
?>
you can print in 2 ways: To
you can print in 2 ways:
To just print something debug style for troubleshooting :
...
# DEBUG/TEST ONLY - ob_ gets the buffer from var_dump()...
# This is not the right way to recover or print fields from $views->result
ob_start();
var_dump($item); #or the $view object, but it is big..
$a=ob_get_contents();
ob_end_clean();
# print above debug
drupal_set_message("debug dump: " . $a, 'status');
OR if you want to print out just the "variable" in the object, you can:
$myvar = $view->node_data_field_dim_big_d_field_dim_big_d_value[0]['value'];
(I think that works.. someone correct me if my syntax is wrong.
In my case, my views result 'row' had 1 field I wanted, and I got it OK using $myvar=$row->recipe_ingredient_name )
Thanks this was very useful
Example of application given here, where products from the same vendor as a featured product are listed below that product, after being gleaned from a view after the method described here, and then displayed using custom display code. Opens up lots of useful possibilities.
Webel, "Elements of the Web", Scientific IT Consultancy,
For UML, UML Parsing Analysis, SysML, Java, XML, and Drupal CMS.
To set filter in custom view
To set filter in custom view call you have to write this.
$display_id='page_4'; //id of view
$view2 = views_get_view( 'View_name' ); //name of view in ui
$view2->set_display('display_id');
$view2->get_total_rows = true;
$item['value'] =array($_GET['tid']); //set value of taxonomy id which is sent by get method in my case
$id = $view2->add_item($display_id, 'filter', 'node', 'term_node_tid_depth'); // calling add_item($display_id, $type, $table, $field, $options = array(), $id = NULL) {
$item = $view2->get_item($display_id, 'filter', $id);
$view2->set_item($display_id, 'filter', $id, $item);
//execute
$view2->pre_execute();
$view2->execute();
$count2 = count( $view2->result ); //do somethint with result, here i count number of results
Been searching this for some time, works in views 2. For names of $table and $field, look into export of view if you have created it, or make some for example to know which are proper names for variables you have to pass.
Drupal tips
Awesome post... I'd been
Awesome post... I'd been looking for that. The parts to this were documented, but I didn't see how it all pulled together.. as in an example. Thank you very much.
Programmatically getting the lowest weight CCK field name
I was trying to get the lowest weight CCK field name in a view. This is useful where you are using views results programmatically. I wanted make sure my code didn't break when the order of fields changed in the view.
The best I could come with was:
$view = views_get_view('your_view_name');
$displayoptions = $view->display["page_1"]->display_options["fields"];
//eg display["block_1"] eg display["page_1"] eg display["default"]
foreach($displayoptions as $key => $value){
if (substr("$key",0,6) == "field_"){
echo "Lightest CCK field: $key";
break;
}
}
TODO: move this handbook
TODO: move this handbook perhaps into views advanced help because this function is in views 2.8
Reverse info?
I'm having this scenario:
In results ($view->result) I have something like this:
<?phpArray
(
[0] => stdClass Object
(
[nid] => 123457832
[node_title] => The title
[node_language] => ro
[node_type] => single_row_ball_bearings
[node_vid] => 123457832
[node_data_field_dim_big_d_field_dim_big_d_value] => 180.000
[node_data_field_dim_big_b_field_dim_big_b_value] => 28.000
[node_data_field_blr_dynamic_cr_field_blr_dynamic_cr_value] => 85.000
[node_data_field_blr_stytic_cor_field_blr_stytic_cor_value] => 79.400
[node_data_field_lsfl_grease_field_lsfl_grease_value] => 3300.000
[node_data_field_lsfl_grease_field_lsfl_oil_value] => 4000.000
)
)
?>
I want to retrieve information about fields, How do I find back the LABEL for "node_data_field_dim_big_d_field_dim_big_d_value"?
__________________
Claudiu Cristea
webikon.com
www.claudiucristea.ro
Try this: $my_view =
Try this:
$my_view = views_get_view_result("my_view_name_goes_here");
$result = $my_view[0]->node_data_field_dim_big_d_field_dim_big_d_value;
Get Field Label
I am wondering the same thing. How do I retrieve the field's label as defined in the view? For instance, if the label for 'node_data_field_lsfl_grease_field_lsfl_oil_value' was set to "Grease/Oil Value" in the view, how would I retrieve this? Thanks!
how to send view args?
How do you send view arguments to this function?
I've setup arguments with my view and i know they work from the live preview.
i've tried:
$view = views_get_view_result('view_name', 'default', 'my-view-arg');
still returns all results.
Sending view arguments
Try:
$view = views_get_view_result('view_name', 'default', array(arg(0),'2ndargument'));As for views_get_view:
$view = views_get_view('view_name');$view->init();
$view->set_display('default');
$view->set_arguments(array(arg(0),'2ndargument'));
print $view->render();
View result only for one page
If the result array should contain all items then set the numbers of items per page to 0 (no limit).
Thank you very much for your post. I did:
$view = views_get_view_result('myview');And I was able to access the view result of one page perfectly. But there must be a way to get more than one page without to set the numbers of items per page to 0.
Maybe somebody have the trick for us?
All the best
Paul
PAULTHEMES
thanks thanks thanks
i have learned a lot with this page.
you made my day!!!
i feel more free with drupal now :)
in drupal 6.20 views 6.x-2.12 if i pass parameter as array, like
$view =views_get_view_result('boletin', 'page_1', array($node->nid));i get the following warning:
# Warning: preg_match() expects parameter 2 to be string, array given in views_break_phrase() (line 806 of /xxx/sites/all/modules/views/includes/handlers.inc).# Warning: preg_match() expects parameter 2 to be string, array given in views_break_phrase() (line 811 of /xxx/sites/all/modules/views/includes/handlers.inc).
so i changed to:
$view =views_get_view_result('boletin', 'page_1', $node->nid);and the warning is gone :)
//trying to answer one question for each one that i make.
//this way, drupal will be more friendly and strong
thx thx ²
keeping track of this for refrence.
tanks * 10000000
now i came back for using in views with views php module. you are a big documentator , specially for people that we have bad english :)
in my view i used in the setup code:
function views_get_current_view_result() {$view = views_get_current_view();
return $view->result;
}
and to get the value:
$info = views_get_current_view_result();
foreach ($info as &$value) {
print $value->node_node_data_field_articulos_secciones_node_data_field_titulo_a_field_titulo_a_value;
}
i put just in case someone reuse it :)
thanks thanks thanks
//trying to answer one question for each one that i make.
//this way, drupal will be more friendly and strong
Multi fields node reference
I would be really great if we could see the values from the multi value fields coming from a node reference.
Nowadays, if your view make use of that, you will not be able to get the values kept in the multi value field.
Is it possible to include these values in this function also?