I needed to get access to the "Global: View result counter". I added it to the fields and excluded it from display, then added a PHP field after it to access it.

Surely enough, it appeared in the available variables as $row->counter. I checked that value and it's an empty string, I checked the $data array and it's didn't contain the counter as well, tried both value and output fields and same result.

Could someone let me know how I can get the row count in the PHP field?

Comments

chunty’s picture

Status: Active » Closed (works as designed)

I don't think this is an issue for this project as such - more one for the main views project - if you exclude the counter from the output its not available full stop - you can't even use it on your own theme template (which drove me crackers for ages!).

The simplest is to not exclude it then to use your own tpl file for the view row which does not display the counter.

bluetegu’s picture

Hi,

The value of global counter is unavailable regardless if it's excluded or not. One can not extract the data in the output either (which is one of the workarounds for http://drupal.org/node/1140896).

I used the following workaround; in my view each row returned fields of a different node, which allowed me to deduce the row number from the nid by placing the following code in the output:

$nid = $data->nid;
$nids = array_map(function($e) { return $e->nid;}, $view->result);
$key = array_search($nid, $nids);

$key is 0 for first row, 1 for second etc., so echo $key+1 is same as printing the global counter.

Hope this will help someone

cattalk’s picture

Sorry ,I cant get it to work. When I enter the above into the output field I get an Internal Server Error.
I tried to use the counter var in the PHP field by putting the counter into the "custom text" field. But this field also returns nothing in the PHP field array. Altough it's listed in the Avaliable Variables.

Thx
cat

bluetegu’s picture

Hi Cat,

The above code works with php 5.3.0 and up (anonymous functions). If your php version is lower, replace:

$nids = array_map(function($e) { return $e->nid;}, $view->result);

with

foreach ($view->result as $e) {
 $nids[] = $e->nid;
}

In any case, make sure you add an echo command to have an actual output (like echo $key+1;).

cattalk’s picture

Ok great! Thanks a lot bluetegu! Seems to work! I totally forgot that this server is running a lower PHP version.

cswan’s picture

Thank you, bluetegu! Your post helped me. :)

shamsher_alam’s picture

Issue summary: View changes

Thank for #2 its working fine for me....