Hi,

I'm developing a module where I'm using the PagerDefault extension, like this:

$application_query = $application_query->extend("PagerDefault")->limit($num_per_page);

The $application_query is rather complicated, with some joins and a bunch of conditionals that apply in certain situations (like admins can see all records, whereas normal users can only see their own records).

At the top of the results page, I need to be able to display something like:

Showing 20 to 29 of 213 Records

But I cannot for the life of me figure out where to get that information from. I've tried looking at both the $application_query object as well as the resulting $application_results object that gets created when I call $application_query->execute(), but I do not see any reference to the total number of objects returned from the database. Clearly the PagerDefault implementation has this information somewhere, and I indeed see that it calls the following:

$total_items = $this->getCountQuery()->execute()->fetchField();

But if I call that from my application code, I get incorrect information and the pager disappears from the web page. What am I missing here? This seems like something that ought to be included in theme("pager") by default, or at least as an option, but I don't see it anywhere.

Comments

alan d.’s picture

Yes, you are on the right track, see theme_pager($variables) and the global $pager_total_items array. Just make sure that you us this after the query has been executed.

[Edit] Note that the pager element is most likely 0, Drupal allows multiple pagers per page, the default is almost always indexed using 0


Alan Davison
tjg’s picture

I've added the following to my code, after the pager query is executed:

global $pager_page_array;
echo "<pre>"; print_r($pager_page_array); echo "</pre>";
global $pager_total;
echo "<pre>"; print_r($pager_total); echo "</pre>";

The query is currently returning 3 rows, so I expect $pager_total[0] to contain the value 3, but it does not. Instead, I get this:

Array
(
    [0] => 0
)
Array
(
    [0] => 1
)

$pager_total turns out to be the number of pages, not the number of records returned, which is not helpful if I want to show the total number of records returned.

So, I'm back to my original question: how to I get the total number of rows that would be returned from a query when it I am using the PagerDefault extension?

alan d.’s picture

The names are all similar

Did you try

global $pager_total_items;
echo "<pre>"; print_r($pager_total_items); echo "</pre>";
# in theme_pager
echo '<p>I think that I have ' . $pager_total_items[$variables['element']] . ' items</p>';

Alan Davison
tjg’s picture

The $pager_total_items variable works for me; that has the information I need. Thanks!

For what its worth, this could be better documented, and I would even advocate for adding the "Displaying x to y of z" to theme("pager") at least as an option.

dongtian’s picture

The $pager_total_items variable works for me; Thank you.

jaypan’s picture

Bad info removed.

Contact me to contract me for D7 -> D10/11 migrations.