« Dataset todo's | Main | Dropdown menus hidden behind Panels Panes in Internet Explorer »

March 16, 2011

Showing total number of items in a view

Wow. What. A. P.I.T.A.

All I want to do is add a header to a View that says the following:

You are viewing [numItems] of [totalItems] records.

and it is nigh impossible.


[crowd snickers]

All you do is modify this simple snippet:


< ?php
$view = views_get_current_view();
$items_displayed = $view->pager['items_per_page'];
$num_rows = $view->total_rows;

You are viewing < ?php print $items_displayed; ?> of < ?php print $num_rows; ?> records.

and you get what you are asking for.

[Not so fast Mister and Miss Opensource...]

If you are limiting the number of items to display, then you need to have a Pager on your view. Problem solved.

[There you go again...]

If your view takes an argument a collision between Drupal Core and Views causes the pager not to render and your total row count is always 1. You might think adjusting the offset would work, but noooo.

If you have Houdini's stomach, you can hack Core. Or, maybe, you want to create a module to accomplish it - because you know your site doesn't have enough modules yet.

[But wait, simple, simpletons...]

The cause of your problem isn't actually the pager, it is the use of distinct in Core which returns a count of 1 which is the problem you were experiencing in the first place.

[Oh yeah. It gets better...]

While having an NID argument can cause the problem, so can having Content Access and/or ACL enabled.

Posted by kkwaiser at March 16, 2011 12:04 PM


Login to leave a comment. Create a new account.