欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

得到views条目数 得到 views数组 总和 总计 大小 自己亲自做的 有大用

shiping1 的头像

把下面的代码放在自定义的模块处  (只不过需要注意命名格式)
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>";
  }
}

然后在
views-view--view_name_goes_here.tpl.php 中调用它就可以了

(要注意的是:当views使用pager才自动计算总行数;如果use_pager=FALSE,需强制指定:$view->get_total_rows = TRUE




来自 https://www.drupal.org/node/347819




 

2) <?php
            //好像应该使用 page_3 而不是 page-3
$content =  views_get_view_result('allcontentview','page_3');
$num = count($content);
print "数目:".$num;//好像是分页的条目
            ?>

3) <?php
 //好像应该使用 page_3 而不是 page-3
//$view = views_get_view ('allcontentview','page_3');
$view = views_get_view('allcontentview');
$view->set_display('page_3');
//$view->set_arguments(array(1,2,3,4,5,6,7,8,9,10));
//这里好像如果设正确参数就可以了,但是动态的参数不知如何设
$view -> get_total_rows = TRUE ;
$view -> execute () ;
$count = $view -> total_rows ;
print "数目".$count ;
?>

<?php 

    $name = 'allcontentview';
    $display_id = 'page_14';
$view = views_get_view($name);
//$arguments = array();
   $view->set_display($display_id);

//下面几行的目的是传参 给filter传参 下面几行有大用
   $item = $view->get_item($display_id, 'filter', 'field_zong_time_value');
   $item['value'] = array('value'=>'2015-08-08','default_date'=>'2015-08-08'); 
   $item['
default_date'] = '2015-08-08'; //最好加上这一句
   $view->set_item($display_id, 'filter', 'field_zong_time_value', $item);
 
$view->pre_execute(); //这一行应该可以注释掉
    $view->execute();
    $result = $view->result;
   //可以不用  $result = $view->result; 而用 //$views->render(); 或者 $view->preview();得到结果
$export = kprint_r($result, TRUE); // $vars - is a variable which you want to print.
     print $export;
 
return $result;

?>

参考的是下面的代码 来自 https://www.drupal.org/node/719330

Ok, I exported the view and checked the filters again.
In my case, the filter "date_filter" has an additional array;

$handler->override_option('filters', array(
  'date_filter' => array(
    'operator' => '<',
    'value' => array(
      'value' => NULL,
      'min' => NULL,
      'max' => NULL,
      'default_date' => '',
      'default_to_date' => '',
    ),

so I needed to wrap the filter into an array.
Working code:

<?php
    $view
= views_get_view('Treffpunkt');
   
$display_id = 'default';
   
$view->set_display($display_id);
   
   
$item = $view->get_item($display_id, 'filter', 'date_filter');
   
$item['value'] = array('value'=>'2010-09-27');       
   
$view->set_item($display_id, 'filter', 'date_filter', $item);

   

$view->set_items_per_page(3);
   
$viewsoutput = $view->render();
    print
$viewsoutput;

   

?>

Hope this helps someone else.

来自 https://www.drupal.org/node/719330


 

Display views results total before embedded view

<?php

$viewName = 'user_reviews';
$display_id = 'block_1';

$view = views_get_view($viewName);
$view->get_total_rows = true;
$view->set_display($display_id);
$output = $view->execute_display($display_id);
$total = $view->total_rows;
print 'total='.$total;

print $output;
?>

来自  https://www.drupal.org/node/693358


Display views results total before views_embed_view()

Hi,

$viewName = 'user_reviews';
$display_id = 'block_1';
print views_embed_view($viewName, $display_id);

I've been using the above code to embed a view. How would I go about retrieving and displaying the total results returned by this view BEFORE it's embedded?

e.g. "There are x number of reviews"
Embedded view...

Thank you for any assistance!!

Comments

rodgolpe’s picture

$viewName = 'user_reviews';
$view = views_get_view($viewName);
$view->execute();
$reviewCount = count($view->result);
print t("There are @reviewCount reviews", array('@reviewCount' => $reviewCount));

$display_id = 'block_1';
print views_embed_view($viewName, $display_id);

来自 https://www.drupal.org/node/677518




Count view query result with multiple views in a page

In a Realty website, I have set up three blocks in the home page for listing the current projects, future projects and completed projects. I have setup three different views to print the project names into each of these blocks. Each of the views print only the top 5 projects in each of the blocks, based on the status of the project.

My requirement is to also print the total number of current projects in that block. I am using block-blockname.tpl.php to print the block as well as the view result count.

Following the solutions suggested in the post - http://drupal.org/node/131031, I have tried using "$view = views_get_current_view();" but it did not work. Further down in the same post, the following snippet was suggested by brian_c, which worked:

<?php

// load view object (has no results yet)
$view = views_get_view( 'name_of_view' );

// set arguments (if needed, otherwise omit this line)
$view->set_arguments( array( 1, 2, 3 ) );

// execute view query
$view->execute();

// results are now stored as an array in $view->result
$count = count( $view->result );

?>

It prints 10 with the view name that i am using for current projects. But there are 27 current projects and the view correctly returns all the 27 projects when viewed in the node. I have tried doing a print_r($view->result) in the home page block, it prints the project names with other values and prints only 10 of them.

Am I using the right method to print the count of view query?
Thanks in advance.

Comments

rodgolpe’s picture

did you try below code:

print $GLOBALS['current_view']->total_rows;
rodgolpe’s picture

Yes. I have tried this method too. But it does not print anything. Why it might be printing only 10 with the $view = views_get_view( 'view_name' ) ? Is there any other approach I can take?

Thanks Rashmi.
Sridhar

rodgolpe’s picture

So bizzare - i'm getting only 10 results as well when using count() - but get all 12 when printing the view!

Any ideas...?

rodgolpe’s picture

Hi,

I've created a view named as "test". It has 8 total records and i set "Items per page"=2. When i tried to run below code

$view = views_get_view( 'test' );

// set arguments (if needed, otherwise omit this line)
$view->set_arguments( array( 1, 2, 3 ) );

// execute view query
$view->execute();

// results are now stored as an array in $view->result
$count = count( $view->result );
//print_r ($view);
print $count;
 

it print the exact total nodes available and that is 8. So i think your code is perfect, please check other settings.

Thanks
Rashmi

rodgolpe’s picture

Is there any way to number raws of the view in spite of using pager? I use full node style view without fields. What must be the code for views-view-row-node--myview.tpl.php to get ordinal number at the top of each node in the view (number not per page, but in the whole view)?

rodgolpe’s picture

Above code returns total number of resulted rows, no matter pager has been set or not. so you can use that in tpl file to print total number of rows.

rodgolpe’s picture

I see, but I don`t need total number, I need number by order of each node in the view (like ordered list without pager).

rodgolpe’s picture

I'm was having the same problem. I found that this code works:

$view = views_get_view($viewname);
$view->set_display($display_id);
$view->set_arguments($args);
$view->execute_display($display_id, $args);
print count($view->result).' rows';

I think it might be to do with $view->execute(); returning results for the the default view, and if the default view is set to display 10 items your count will always be 10, regardless. Specify the display id (eg. 'page_1') where you've set unlimited items to display, and you'll get the total number of rows for your view.

At least, that's my interpretation!

来自 https://www.drupal.org/node/508130





 

How to get the total count / row count / result count of a view? E.g., get total count of all articles posted by a User (UID)

<?php
    $count = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] - 1 : 0;
    $pager = $this->view->pager;
    // Get the base count of the pager.
    if ($pager['use_pager']) {
      $count += ($pager['items_per_page'] * $pager['current_page']) + $pager['offset'];
    }
?>

$view = views_get_current_view(); print 'Item number: ' . $view->total_rows . '';




global $user; $result = db_query("select * from {node} where uid = $user->uid"); while ($row = db_fetch_array($result)) { $type=$row[type]; ${"number_of_type_".$type}+=1; } print "You have written ".${"number_of_type_page"}." page posts"; print "You have written ".${"number_of_type_blog"}." blog posts";



<?php $view = views_get_current_view(); print 'Total rows: ' . $view->total_rows; ?>


$view = views_get_current_view(); print 'Results: ' . $view->total_rows / 3 . ' Pages';



来自 https://www.drupal.org/node/534714

 












普通分类: