Core\Ldap\Entries::total
$entries->total(): ?intReturns how many entries a sorted search found in total, when the search used the window option.
A search with sort and window returns one page of a sorted list. offset says how many entries to skip, and window says how many to return. The server also counts the whole list, and total returns that count. Use it to print "page 2 of 5" or "showing 11 to 20 of 48".
The count is the same for every page, and it does not change while the loop reads the entries.
Good to know: a search without window returns null. The server did not count the entries, so you count them yourself in the loop.
Return value
Returns ?int. The number of entries, or null when the search did not use window.
Examples
The examples below run with this configuration file.
# These examples use the LDAP directory server on this computer at port 16636.# `tls_ca_file` is the certificate that the server's certificate is checked against.[ldap.corp]url = "ldaps://127.0.0.1:16636"base = "DC=example,DC=test"user = "CN=Reader,OU=Company,DC=example,DC=test"password = "Novis-reader-1"tls_ca_file = "../../../../../tests/db/ca.crt"timeout = "5s"
# The program may open the `corp` directory.[capabilities.ldap]connect = ["corp"]Show a page number
<?nvs// Shows how to print "page 1 of 3" above one page of a sorted list of people.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');int $perPage = 2;
// `sort` and `window` make the server return the first two people by name.var $people = $corp->search(Core\Ldap\Filter::equals('objectClass', 'user'), { base: 'OU=Company,DC=example,DC=test', select: ['cn'], sort: 'cn', window: $perPage,});
foreach ($people as Core\Ldap\Entry $person) { echo $person->string('cn') ?? '', "\n";}
// `total` is the number of people in the whole list, not only on this page.// There are 5, so this prints "Page 1 of 3".int $count = $people->total() ?? 0;int $pages = Core\Math::ceil($count / $perPage) as int;echo 'Page 1 of ', $pages, "\n";Anna BergBen KohlPage 1 of 3No window means no count
<?nvs// Shows that `total` is `null` for a search without the `window` option.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
// This search reads every person page by page. It has no `window`.var $people = $corp->search(Core\Ldap\Filter::equals('objectClass', 'user'), { base: 'OU=Company,DC=example,DC=test', select: ['cn'],});
// The server did not count the entries, so this prints "not counted".?int $total = $people->total();echo $total == null ? 'not counted' : 'counted', "\n";
// To count them, read them all. This prints 5.int $count = 0;foreach ($people as Core\Ldap\Entry $person) { $count = $count + 1;}echo $count, "\n";not counted5The last page
<?nvs// Shows how to read the last page of a sorted list, and to print which entries it shows.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');int $perPage = 2;int $offset = 4;
// `offset` skips the first four people, so this page starts with the fifth.var $people = $corp->search(Core\Ldap\Filter::equals('objectClass', 'user'), { base: 'OU=Company,DC=example,DC=test', select: ['cn'], sort: 'cn', offset: $offset, window: $perPage,});
int $shown = 0;foreach ($people as Core\Ldap\Entry $person) { echo $person->string('cn') ?? '', "\n"; $shown = $shown + 1;}
// The last page has one person. This prints "Showing 5 to 5 of 5".echo 'Showing ', $offset + 1, ' to ', $offset + $shown, ' of ', $people->total() ?? 0, "\n";ReaderShowing 5 to 5 of 5