16

How do I get the actual rows number in views-view-field--title.tpl.php in Drupal7 and views3?

I know how to get the total by

$view = views_get_current_view();
$view->total_rows;

But how do I get the actual row number? To print 1/4 2/4 3/4 4/4

1

4 Answers 4

13

user1090003 has the right answer.

in views-view-fields.tpl.php or... you can have the row index like this $view->row_index

Further explanation:

In views-view-field--title.tpl.php (suggestion of views-view-fields.tpl.php), you already have the $view object available. That view object has a property "row_index" that equals the current row number starting at zero.

Example of use:

    <?php print $view->row_index; ?>
    <?php print $output; ?>

Example results:

    0
    Node Title 1
    --
    1
    Node Title 2
1
  • This also works in a node--view--<my-view>.tpl.php Sep 27, 2016 at 18:33
3

I usually accomplish this by adding a "count" field and making it excluded from display. In the title field I add the token for the "count" field in "rewrite output." Make sure the count field is BEFORE the title field.

1
  • 3
    I use field Global PHP as numbering column and there I put echo ($view->row_index); works well in Drupal 7 (but counts from ZERO). May 18, 2014 at 11:35
2

in views-view-fields.tpl.php or... you can have the row index like this $view->row_index

1
  • Hello. Could you please explain a bit, and maybe provide code sample?
    – Mołot
    Sep 1, 2013 at 16:16
0

Setting the $static var to 0 in the VALUE CODE area allows to do $static++ in de OUTPUT CODE area

2
  • 2
    Could you elaborate on this a little?
    – mpdonadio
    Jan 9, 2014 at 1:58
  • In views there are two fields when using the Global PHP field. You can set pre and post render data in these. This works just fine for a simple counter. Aug 14, 2018 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.