I just stumbled upon this module--after banging my head trying to figure out how to add an 'index' to the row output in a list view. Thanks!

My view list is grouped by a field such that my output looks like this:

<h3>taxonomy term</h3>
<li>item 1</li>
<li>item 2</li>

Currently rownumber is calculated based upon the total number of rows returned regardless of grouping. Any chance this could be calculated within the grouped field?

thx much


ppmax’s picture

Back again to comment on this module and what I'm trying to do. I posted this in the general forum but think it's appropriate here (http://drupal.org/node/354091):
I'm trying to wire together a views list full of node titles and the viewscarousel (jCarousel) start position. By passing a start index to my viewscarousel view I can get my carousel to start from the current node instead of from the beginning each time.

Here's some background:
My view outputs an unordered list grouped by taxonomy term. Within each term is a list of node titles. I've modified the views field output via a tpl file so that I can link the node title to specific sections on my site. Heres some pseudo code/output of this view:

<h3>Taxonomy Term1</h3>
<li><a href="path/to/someplace/nodetitle_foo"></li>
<li><a href="path/to/someplace/nodetitle_baz"></li>

What I'd like to do is append an index after each node title like so:

<li><a href="path/to/someplace/nodetitle_foo/1"></li>
<li><a href="path/to/someplace/nodetitle_baz/2"></li>

I'll then grab that index then call jcarousel({start:<?php print arg(2); ?>}); at document.ready.

It would be trivial to add an index at the list.tpl level--the problem is that the custom link I build is in the field tpl file--which knows nothing about how many items there are in the array of results. IOW:
list.tpl.php file:

<?php foreach ($rows as $id=>$row): ?>   //I could add $i=1; here...
<li><?php print $row; ?></li>
<?php endforeach; ?>    //I could add $i++; before the endforeach; here...

field.tpl.php file:

<a href="path/to/someplace/"><?php print $node->title; ?><?php print $node->title; ?></a>    //How can I print $i here??

WRT my previous post and this module I think an ideal solution would be to be able to arbitrarily append the $row to include additional elements in an array such that each array element could be called anywhere within the field $data output. For example I created a field in my view to return node id. In my field.tpl.php file I do:

$data = $row->{$field->field_alias};
$node_obj = node_load($data);
$categories = paulperreault_ppterms($node_obj);  //this function returns taxonomy terms for the node
$category = $categories[0];


<a <?php print $class; ?> href="/photography/<?php print $category; ?>/<?php print check_plain($node_obj->title); ?>"><?php print check_plain($node_obj->title); ?></a>

...which will output a link to the node within my view. I'm trying to figure out a way that I can do something like this in the field.tpl.php file:

$index = $row['custom_field_data']['mydata'];  //this could be rownumber in the modules current incarnation


<a <?php print $class; ?> href="/photography/<?php print $category; ?>/<?php print check_plain($node_obj->title); ?>/<?php print $index; ?>"><?php print check_plain($node_obj->title); ?></a>

Using this as a model views custom field data would be flexible enough so that you could create things like:
$row['custom_field_data']['prepend'] //could contain markup that can be used anywhere
$row['custom_field_data']['append'] //could contain markup that can be used anywhere
$row['custom_field_data']['args'] //could contain an arg()

I've checked out the module code and include files and while I can grok what youre doing I'm "not quite there yet" as far as my drupal module or drupal api skills and cant figure out how I'd change or extend views code to do this.

Any tips or suggestions would be appreciated.

casey’s picture

with customfield PHPCode you can already add custom fields to rows, or isn't that what you're looking for?

ppmax’s picture

Thanks for the reply Casey--

I see how I can add custom fields using the PHPCode field type, but I need the output of that field to iterate depending on the number of rows returned. Using the current feature I could add a field 'stuff' with value 'thing' but I cant (for example) return rownumber. IOW, Customfield:rownumber is a separate field returned for each row; I want to grab the value of $rownumber for use within the output of another field. Ultimately I want to return a rownumber within the nid.tpl.php file because I'm using rownumber as a way to pass a rownumber index to some javascript.

Pseudo code for nid.tpl.php:

<a href="/photography/dogs/<?php print $node->title; ?>/<?php print $rownumber; ?>">row</a>

...which will output a URI:

Am I not seeing something obvious?

thanks again,

casey’s picture

ah, I think I get it. What you could do (you won't have to change/add .tpl files):

1. set the field you want to wrap the url (containing a rownumber) around to "exclude from display". I guess you want this for "Node:title".
2. Add an Customfield:PHPCode with code:

<a href="/photography/dogs/<?php print $data->node_title; ?>/<?php print ++$static; ?>">row</a>

Available variables for Customfield:PHPCode (also below settings-form as helpmessage)

$data: contains the retrieved record from the database (e.g. $data->nid).
$static: can be used to store reusable data per row.
$this: field handler of PHPCode (you probably won't need this one)
$this->view: whole view object (you probably won't need this one)

Hope this does the job for you.

ppmax’s picture

Status: Active » Fixed

Casey: YOU are the man. That's an interesting and nice technique and solves my issue.

Regarding my first question (see original post) about calculating returning the $static value per any field grouping that may have been set in the view HTML List Style control: I'm going to go scratch my head on that one and close this support request. If you don't mind I may return and post a support request about that separate issue.

Thanks again!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

casey’s picture

Category: feature » support
soulfroys’s picture

Building a on-the-fly ranking:

//echo '
'; print_r($data); echo '

$rank = $static[0]; // last
$points_1 = $static[1]; // last
$points_2 = $data->userpoints_txn_points; // current

if ( $rank > 0 ) {
if ( $points_1 > $points_2 ) {
else {
$rank = 1;
$static = array( $rank, $points_2 );
echo $rank;

krueschi’s picture

For quite some time now I tried to figure out how to number rows displayed by views in a "sports ranking" style.
What do I mean?
Imagine you have some kind of points that indicate the rank of something, for instance sport scores of some kind.
The order of fields is easy and the rownumber plugin style works well as long as there will be no score twice ... (To stay close with my sport scores example: If there are 2 athlets with the same high score they will be both at 1st place, the next best will not be the 2nd but become 3rd!)
So I played a little bit with the code from #8 and found this working for me:

//echo '<pre>'; print_r($data); echo '</pre>';
$rank   = $static[0]; // last
$points_1 = $static[1]; // last
$nbrow  = $static[2]; // row count helper
$points_2 = $data->userpoints_txn_points; // current

if($nbrow > 0) {
else $nbrow = 1;

if($rank > 0) {
  if ($points_1 > $points_2) {
else {
  $rank = 1;

if($points_1 != $points_2) {
  $rank = $nbrow;
  $points_1 = $points_2;

$static = array($rank, $points_2, $nbrow);
echo $rank;

Maybe this could be of any help to anyone else too.