User Authentication With patUser (part 3) - Natural Selection (
Page 4 of 7 )
You've already seen,
in my examination of the getUsers() and getGroups() methods, that you can apply
selection criteria (as the second argument to those methods) to return a result
subset containing only the records you need. Here's an example:
<?php
$list = $u->getUsers(
array("uid", "username"),
array( array("field" => "sex", "value" => "F", "match" =>
"exact") ) );
?>
The code snippet above would return a list of only those
users who were female.
There are three components to the second argument
in the snippet above: the field, the value, and the selection criteria. The
first two are fairly obvious: the "field" key specifies the field against which
to match, while the "value" specifies the value against which records are to be
compared. The critical element, though, is the third key, "match", which defines
how the comparison between the "field" and the "value" will actually be
performed.
patUser permits comparison using any of the following
conditions:
"match" => "exact" field value equals "value" when
compared in a case-sensitive manner
"match" => "greater" field value
must be greater than "value"
"match" => "lower" field value must be
lower than "value"
"match" => "greaterorequal" field value must be
greater than or equal to "value"
"match" => "lowerorequal" field value
must be lower than or equal to "value"
"match" => "contains" field
value contains "value"
"match" => "like" field value equals "value"
when compared in a case-insensitive manner
"match" => "startswith"
field value starts with "value"
"match" => "endswith" field value
starts with "value"
"match" => "between" field value is between the
"value" array (x,y)
The following snippet would return the IDs of all
users whose username contains an "e",
<?php
$list = $u->getUsers(
array("uid"),
array( array("field" => "username", "value" => "e", "match" => "contains") ) );
?>
while this one would return usernames and email addresses of
all those users who have logged in more than 11 times and are over 18 years old:
<?php
$list = $u->getUsers(
array("username", "email"),
array(
array("field" => "count_logins", "value" => "11",
"match" => "greater"),
array("field" => "age", "value" => "18", "match" =>
"greater") ) );
?>