views_embed_view - how to pass views argument ?

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by WuffWuff on December 8, 2008 at 10:53pm

I am working with Drupal 6.6 and the new views 2 module. I am trying to output a view after executing some PHP code. My site has a form that enables visitors to search nodes - a company and a program node. One company can have many programs so I have used the node reference function to create a mini relational database. An example of a search might be to choose all programs that are in Argentina, are under 1000ドル and last 5 weeks. The php part that builds the sql works fine and the results of the query are Node IDs.

So it should be simple to just feed the Node IDs into a view and then get the view to spit out the data. I guess I am not understanding how to feed the list of Node IDs into the views argument. This is what I am using:

$output = views_embed_view("search", "page", $myview_args) ;
print $output;

My view has an argument set as Node: Nid

So I basically have a list of Nod ids I'm going to put in $myview_args. How do I place them in this variable. I have tried:
$myview_args = "23, 44, 100" and
$myview_args = "23 44 100" and
$myview_args = array("22 44 100)
$myview_args array("Nid => 363");

I don't seem to be able to pass the argument into the function ? I can go to my view, enter Node ids as arguments and do the live preview thing and it works fine. Your suggestions?

Comments

embed_view arguments

Posted by pasqualle on December 9, 2008 at 12:12pm
<?php
print views_embed_view('search', 'page_1', array(23,44,100));
?>

check the display_id, you are most probably using the display name. that's not the same..

Still no Joy

Posted by WuffWuff on December 9, 2008 at 3:07pm

if I place node ids from the output of my code in the "Live Preview" arguments section I get multiple values out, the view appears to work fine. If I use the code above, I get only the first record outputted and now an error about the array???

warning: preg_match() expects parameter 2 to be string, array given in /home/planetgapyear.com.dev/sites/all/modules/views/includes/handlers.inc on line 767.

Have I uncovered a bug here? My only option seems to wrap the view_embed_view in a where loop and feed the node id into ever pass of the loop. Has the problem got something to do with the node reference field being used?

You either

Posted by merlinofchaos on December 9, 2008 at 3:41pm

You either want:

views_embed_view('viewname', 'page_1', 1, 2, 3)

or

views_embed_view('viewname', 'page_1', '1+2+3')

depending on if you have a single argument expecting multiple nids or multiple arguments each expecting a single nid.

In general the format should be exactly the same as you used in preview, except that any slashes used in preview will be removed.

it works !!!

Posted by aakanksha on July 24, 2012 at 7:01am

it works !!!

Nope :(

Posted by WuffWuff on December 9, 2008 at 4:08pm

I only get one result displayed, what am I doing wrong? When I go to the edit views page and throw the args into the Live Preview it spits then all out!!!

display id

Posted by pasqualle on December 9, 2008 at 4:41pm

did you checked the display id? because "page" does not seems as a good id for me, and in this case the default display will be used, as I know..

To figure out the id of a display, hover your mouse over the tab to select that display. Everything after the '#views-tab-' is the id of that display. This ID is guaranteed never to change unless you delete the display and create a new one.

Display ID is correct

Posted by WuffWuff on December 9, 2008 at 5:52pm

The display id appears to be correct. Is there something I am not doing in my view to limit the display to one record when I use views_embed_view? Should I get the latest dev version, I am using 6.x-2.1. Can anyone confirm that this code actually works ?

Working !!

Posted by WuffWuff on December 9, 2008 at 6:20pm

Ok seems to like + rather than commas and I swear I tried this before and it didn't work, some kind of cache issue ??? Here was my code that got it working. Views does not like it if you have an extra + on the end of the argument.

$nodID = "";
$count = 0;

while ($row = mysql_fetch_array($query_result)) { //the result of a complex query .......
$a = $row["nid"];
if ($count == 0 ) $pre = "" ;
else $pre = "+" ;

$count = $count +1;
$nodID = $nodID.$pre.$a; //populate the $nodID to pass to views_embed_view function
}

print ("$nodID"); // debug

$viewName = "search_out";
$display_id = "page_1";
print views_embed_view($viewName, $display_id , $nodID);

You could simplify this

Posted by marc delay on June 10, 2009 at 5:13pm

You could simplify this quite a bit with this code:

$a = array();
while ($row = mysql_fetch_array($query_result)) { //the result of a complex query .......
$a[] = $row["nid"];
}
$nodID = implode("+", $a);
$viewName = "search_out";
$display_id = "page_1";
print views_embed_view($viewName, $display_id , $nodID);

how do you check the view

Posted by mediamash on March 12, 2010 at 10:06am

how do you check the view content if empty?

<?php
if (views_embed_view("content_link","block_3")->result):
print
views_embed_view("content_link","block_3");
endif;
?>

That's certainly one way to do it

Posted by rich.yumul on March 12, 2010 at 12:34pm

But I'd store the view output in a variable first so you don't incur the overhead of running the view twice:

<?php
$output
= views_embed_view("content_link","block_3");
if (
$output){
print
$output;
}

?>

Rich Yumul
Sage Tree Solutions
www.sagetree.net

Yep, nice one rich. That's

Posted by ludo1960 on March 12, 2010 at 3:44pm

Yep, nice one rich. That's the way to do it!

...okay but if it doesn't work

Posted by whikloj on April 6, 2010 at 2:06pm

I like this and it makes sense, but I have tasked with trying to make empty tabs on a view disappear. So I tried a custom page template with

<?php
$viewsoutput0
= views_embed_view("Subject_Hub",'page_1',411);
$viewsoutput1 = views_embed_view("Subject_Hub",'page_2',411);
$viewsoutput2 = views_embed_view("Subject_Hub",'page_5',411);
?>

and then...

<ul class="tabs primary">
<?php
if ($viewsoutput0){ print '<li><a href="#typeA">Databases</a></li>';}
if (
$viewsoutput1){ print '<li><a href="#typeB">Librarian</a></li>';}
if (
$viewsoutput2){ print '<li><a href="#typeC">Course Guides</a></li>';}
?>

</ul>

Only the first view provides any content, but all 3 tabs appear. I feel I am missing a basic step....

not pretty but it works

Posted by whikloj on April 9, 2010 at 6:24pm

This is not that best way but based on suggestions found in http://drupal.org/node/426114, I created a custom block to create tabs based on the views and then turned off the tabs in the actual view.

how to receive view_embed_view arguments

Posted by narendrak on April 9, 2010 at 11:19am

Hi,

I am calling view2 in view1.

In view1 i created Customfield: PHP code. and Call to view2 with additional argument '2010' like

views_embed_view($viewName, $display_id, array(2010)); //i am using drupal 6.

how to receive view_embed_view arguments in view2?

In View 2 i created argument field year (which is CCK field stored value as year) and still not working
i want to apply filter on view2 on the basis of year value.

where & how i can receive year value in view2??

Thanks in advance
Narendra

I'm trying something

Posted by Neli on August 10, 2010 at 2:42pm

I'm trying something similar.

I have 2 views. In one (view1) there are the interests of people (selected by the user when registered).
And in the other one I have events (view2).
I need to send the results from view1 to the arguments of view2.

But I'm trying to do this with PHP.

<?php

global $user;
$args = $user->uid;
$display = 'default';

$view_interests = views_get_view('user_interests');
$view_interests->set_arguments($args);
$view_interests->build($display);
$view_interests->execute($display);
$view_interests->render();

$view_event = views_get_view('section_events');

foreach (
$view_interests->result as $variant) {
print
$view_event->execute_display('default', $variant);
}
?>

This gives the following error:
Fatal error: Cannot use object of type stdClass as array in ...\sites\all\modules\contrib\views\includes\view.inc on line 460

But if I only use view1 and print this one in the foreach:

<?php
foreach ($view_interests->result as $variant) {
print
$view_interests->execute_display('default', $variant);
}
?>

That does work. It prints view1 the amount of times that there are results in it.
I really have no clue how to fix this.
Can anybody help me please? My knowledge of Drupal isn't that great yet.

There is typo here: global

Posted by fobd on December 10, 2010 at 2:34pm

There is typo here:

global $user;
$args = $user->uid;
...

You need:

$args = array($user->uid);

It looks like you're passing

Posted by wjaspers on November 11, 2010 at 4:44pm

It looks like you're passing an object to an array parameter.

I thought typecasting stdObject to an array might work, but it didn't for me.
Typecasting is like this:

<?php
$object
= $someObject; //some object
$values = (array)$object; // now we have an array
?>

I'm assuming your code:

<?php
foreach ($view_interests->result as $variant) {
print
$view_interests->execute_display('default', $variant);
}
?>

is sending $variant as the object.

EDIT: The typecasting trick didn't work for me (Views 2.11); but you can still pull properties from stdObject to pass to your "interests" view. If you know what properties you want, just call execute_display('default', $variant->field); or whatever is appropriate.

Workaround for arguments bug, found by trial and error

Posted by francis55 on September 2, 2011 at 7:19am

Using Drupal 6
I had a similar problem with a view using arguments
* the preview worked: I could pass arguments like arg1 or arg1/arg2 or arg1/arg2/arg3
* but in the views_embed_view rendering, the corresponding command print views_embed_view(viewname, block_1, arg1, arg2); did not work
I found that by adding exposed Filters (allowed values) and allowing all the values in the list overcame the problem.
Looks like a bug hidden somewhere deep in the code.

I solved this by rewriting

Posted by TuFrac on February 21, 2012 at 1:42am

I solved this by rewriting the function:

$args = array(arg1, arg2, arg3, ....);
function _views_embed_view($name, $display_id = 'default', $args) {
$view = views_get_view($name);
return $view->preview($display_id, $args);
}

In case anyone still has the problem, let me know it it works,

You can use this way

Posted by bongdua on October 18, 2012 at 7:52am

You can use this way :

<?php
$arg
=array (1,2,3);

print
views_embed_view('search', 'page_1', implode('+',$arg)); // OR

print views_embed_view('search', 'page_1', implode(',',$arg)); //AND
?>

pass argument into views pager

Posted by phponwebsites on April 2, 2015 at 6:12am

How to pass argument into pager in views?
Actually the pager url looks like http://www.example.com/sample?page=2
I want also pass some extra argument with it.
How to pass arguments?
My url should be http://www.example.com/sample?page=2&type=user

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /