I've been Googling my fingers to the bone, and trying all variations of:

$view1 = views_get_view('activity_center_events_page');
$view1->get_total_rows = true;
$view1->execute();

$viewrows = $view1->get_total_rows;

if ($viewrows > 0) {
  echo 'rows...';
}

Unfortunately, all for not... can someone enlighten me on simply how to determine if a view has no results? I am trying to manually embed two views, and depending on if the first has any results, I'll display the second.

Oh, and I've tried setting the empty string in the view to text as well as deleting the contents.

Comments

steveadamo’s picture

just for grins, i've also tried checking for a result from

$view = views_embed_view('activity_center_events_page', 'default', $node->nid);
if ($view) {
  print 'rows...';
}

this failed as well, and i think ive fallen into desperation coding, as i know im being horribly innefficient now...

steveadamo’s picture

hmm... looking into this thread now...

http://drupal.org/node/446798

nevets’s picture

Here is one approach, make a view that represents block 1, add an attachment that represents block 2.

Now make a modified copy of the theme file for "Display output". I would pick one of the names specific to the view. The default theme file has a part like

  <?php if ($attachment_after): ?>
    <div class="attachment-after">
      <?php print $attachment_after; ?>
    </div>
  <?php endif; ?>

Change to

  <?php if (empty($rows) && $attachment_after): ?>
    <div class="attachment-after">
      <?php print $attachment_after; ?>
    </div>
  <?php endif; ?>

to have the attachment only show when the block view is empty. You can also do the same for $attachment_before.

steveadamo’s picture

interesting approach... im not entirely sure how to create an attachment, but ill look into it...

out of curiosity, why is this code snippet not returning the expected results?

$view = views_get_view('foo');
$view->set_display('default');
$output = $view->preview();
// At least in $view->result is the result.
if ($view->result) {
  print 'results!';
}
steveadamo’s picture

i dont think this approach will work in my case, as the views take different arguments (and display fairly different sets of data), and it seems as though the nature of an attachment is to inherit from its parent...

im just baffled as to why the code snippet i posted above is not working in my case...

shaneonabike’s picture

Awesome! This is such a great trick and helped me out immensely! Thanks for posting it... :)

sapark’s picture

If you have, for example, nid as one of your fields, you could check if a nid was returned:

// Load a view by name
$view = views_get_view('draft');
// Get an array of view result objects
$view->render();
// Get first nid in results
return ($view->result[0]->nid);
jh3’s picture

Check out this thread and see if what I posted works for you as well. It seems to work for me so far.

Hunabku’s picture

for a view that creates a table i just did a pregmatch to see if there is a "table" tag. Then i know its not empty.

if (preg_match("/<table/i", $view)) {
    $not_empty = true;
} else {
    $not_empty = false;;
}
herb’s picture

Thank You!! I've been trying to use views_embed_view in a Tab to display videos. But I didn't want to see the Tab if the view was empty! The preg_match technique did the trick. (Although I wonder if there is a more elegant solution?)

2dareis2do’s picture

this worked for me too.

aklump’s picture

This is the approach I use when i need to determine if a view is empty or not, this code allows for arguments and different displays. If no arguments then just omit the set_arguments line.

<?php
$view = views_get_view($view_name);
$view->set_arguments(array($arg1, $arg2));
$view->execute($display_id);
if (!empty($view->result)) {
  //the view has a result...
}
?>

If you want to get the output of the view inside the if statement, you can just add this line, since the display and arguments are already set. Rather than using views_embed_view().

$output = $view->preview();
hlopes’s picture

... but here it only works if i have advanced content profile kit active..

Either the displayed above as well as the $view->preview() thing.

Strange uh.

yaworsk’s picture

in the event this helps anyone, i just used,

$name='YOUR_VIEW_NAME';
$tid = arg(1);
$view = views_embed_view($name, 'default', $tid); //default is the title of the view I am using, i.e. page, block, etc.
if (strpos($view, 'views-field-field-video-description-value')) {    //this is the key, I have a cck field for video description. if that shows up in the view, i know i have results.
print $view;
} else {
$view = views_embed_view($name, 'default'); //if the cck field doesn't show up, i dont have results and it will use the default argument for the view, which in my case is all. You could pass another argument though...
print $view;
}

Hope this helps!
pete