Richard Jones Richard Jones - 1 year ago 58
MySQL Question

How to count users in table with php

in wordpress custom php this code works:

$result = count_users();
echo 'There are ', $result['total_users'], ' total users';
foreach($result['avail_roles'] as $role => $count) {
echo $count, ' are ', $role, 's', ', ' . "<br />";
echo '.' . "<br />";

Here is an example of output by that:

There are 14 total users

1 are administrators,

3 are s2member_level1s,

13 are access_s2member_ccap_video,

13 are access_s2member_ccap_saas,

1 are access_s2member_ccap_optins,

10 are subscribers,

So, is there a way to count all users with a role, like say: s2member_level1s, or access_s2member_ccap_saas with just one call, or do I have to do it like that, then count the foreach results, every time?

I am hoping there is a way to just count them with one pull, so it does not take a lot of resources and time for the page to load, everytime we load the page.


Answer Source

Since you're looping through the results and print the key of each one, you should be able to access directly the result using the key:

$result = count_users();
echo $result['avail_roles']['s2member_level1s'];

Here's more information on arrays:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download