Drupal return number of results in a View

前端 未结 6 1154
执念已碎
执念已碎 2021-02-07 19:47

I have a view in Drupal that filters my content. It brings back 7 rows. All I want to return is the number or results returned(7). Is this possible?

I tried using the Vi

相关标签:
6条回答
  • 2021-02-07 19:54

    If using views_get_view in Views 3, you can use this snippet:

        $view = views_get_view('MY_VIEW');
        $view->set_display('MY_DISPLAY');
        // Execute first
        $result = $view->preview('MY_DISPLAY');
        // Output result only if rows > 0
        if (count($view->result) > 0) {
          print $result;
        }
    
    0 讨论(0)
  • 2021-02-07 19:57

    With Views 3 you may need to do

    $view->get_total_rows = TRUE;
    $total_items = $view->query->pager->get_total_items();
    
    0 讨论(0)
  • 2021-02-07 20:00

    with drupal 7 - Under the pager options you have the option to Expose items per page When checked, users can determine how many items per page show in a view

    0 讨论(0)
  • 2021-02-07 20:06

    If you want to get the count outside the view you can use this

    $view = views_get_view('MY_VIEW_NAME');
    
    $view->set_display('MY_DISPLAY'); // like 'block_1'    
    
    $view->render();   
    
    print sizeof($view->result);
    

    Note : Do not use this within a view. It will be an overhead. If you are using it within view check the other answers.

    0 讨论(0)
  • 2021-02-07 20:11

    what you can do is to activate php for the views header/footer and add the following snippet to it:

    <?php
      $view = views_get_current_view();
      print $view->total_rows; 
    ?>
    

    This will print the total number of rows.

    If you need the result as a field, you could use the "Views custom field" module, add a php field and run the same snippet.

    Regards

    Mike

    0 讨论(0)
  • 2021-02-07 20:14

    This works well for me and deals with the pager issues. Put this function in your custom module, rename / format as needed, and call it from your views-view--*view_name_goes_here*.tpl.php files.

    function get_view_rowcount(){
    
     $view = views_get_current_view();
     $page_total = count($view->result);
     if(isset($view->total_rows)){
       return "<strong>Displaying " . $page_total . " of " . $view->total_rows . " total rows.</strong>";
     } else {
      return "<strong>Displaying " . $page_total . " of " . $page_total . " total rows.</strong>";
     }
    }
    
    0 讨论(0)
提交回复
热议问题