note 41472 added to function.ldap-sort
| From: | john at benden dot co dot uk | Date: | Sun, 11 Apr 2004 20:30:29 +0000 |
| Subject: | note 41472 added to function.ldap-sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-67988@lists.php.net to get a copy of this message | ||
An alternative for multivalue sorting for ldap queries:
function cmp($a,$b) {
global $fld1,$fld2,$fld3; // getting the fld defs from the main routine
global $info; // get search results from the main routine
$d=strcasecmp($info[$a][$fld1][0],
$info[$b][$fld1][0]);
if ($d==0) {
$d = strcasecmp($info[$a][$fld2][0],
$info[$b][$fld2][0]);
if ($d==0) {
$d= strcasecmp($info[$a][$fld3][0],
$info[$b][$fld3][0]);
}
}
if ($d==0) {
return 0;
} else {
return ($d>0)? 1 : -1;
}
}
//
// connect to the directory server
//
$ds=ldap_connect("localhost",389);
ldap_bind($ds,"","");
//
// setup the search
//
$basedn="o=org";
$search="(sn=foo)"
$searchattr=array('sn','givenname','desc','telephonenumber',
'cn','mail');
//
// do the search
//
$sr=ldap_search($ds, $basedn, $search,$searchattr);
//
// verify that the search was ok
//
if (ldap_count_entries($ds,$sr)==0) {
echo "<BR><H1>No results</H1>";
} else {
// some results found
// get the search results
$info = ldap_get_entries($ds, $sr);
// setup the sorting fields
$fld1="sn";
$fld2="givenname";
$fld3="desc"; // there maybe a better attribute
uksort($info,"cmp"); // use a key sort
//
// Setup results
//
echo "<table><tr><th>Name</th>";
echo "<th>Phone</th> <th>Mail</th>";
echo "<th>desc</th> </tr>";
while (list($i, $value) = each($info)) {
echo "<tr><td>" . $info[$i]["cn"][0] .
"</td><td>";
echo $info[$i]["telephonenumber"][0] . "</td><td>";
echo $info[$i]["mail"][0] . "</td><td>";
echo $info[$i]["desc"][0] . "</td></tr>";
}
echo "</table>";
ldap_free_result($sr);
}
ldap_close($ds);
i hope this helps!
----
Manual Page -- http://www.php.net/manual/en/function.ldap-sort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41472
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41472&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41472&report=yes
Search -- http://master.php.net/manage/user-notes.php