note 50644 modified in function.ldap-sort by danbrown

From: Date: Wed, 31 Mar 2010 16:22:47 +0000
Subject: note 50644 modified in function.ldap-sort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167573@lists.php.net to get a copy of this message
It is interesting people are using all these methods to sort multi-attribute results from LDAP searches. Well, here is a simpler one. Just use ldap_sort once for each sort attribute in the reverse order of importance. --hikmet <?php $ldapFilterAttributes = array ('givenname', 'sn', 'mail'); $ldapSortAttributes = array('sn', 'givenname'); // ie. sort by givenname, then by sn $ldapFilter = "(&(sn='')(givenname=''))"; $ldapBaseDN = 'ou=users, dc=example, dc=com'; $search = @ldap_search($ldapConnect, $ldapBaseDN, $ldapFilter, $ldapFilterAttributes) ; if (!($search)) { die("Unable to search LDAP server"); } // lets sort the results by firstname if firstname is in the results foreach ($ldapSortAttributes as $eachSortAttribute) { if (in_array($eachSortAttribute, $ldapFilterAttributes)) { // making sure we don't accidentally try to sort against an inexisting field ldap_sort($this->connect, $search, $eachSortAttribute); } } // and here is the result $results = ldap_get_entries($this->connect, $search); ?> [EDIT BY danbrown AT php DOT net: Bugfix by (huntz AT huntz DOT org) on 17-MAY-2005: Nice tip, but on line 2 of your example we need to use $ldapSortAttributes and not $ldapSortAttribues :-)] --was-- It is interesting people are using all these methods to sort multi-attribute results from LDAP searches. Well, here is a simpler one. Just use ldap_sort once for each sort attribute in the reverse order of importance. --hikmet <?php $ldapFilterAttributes = array ('givenname', 'sn', 'mail'); $ldapSortAttribues = array('sn', 'givenname'); // ie. sort by givenname, then by sn $ldapFilter = "(&(sn='')(givenname=''))"; $ldapBaseDN = 'ou=users, dc=example, dc=com'; $search = @ldap_search($ldapConnect, $ldapBaseDN, $ldapFilter, $ldapFilterAttributes) ; if (!($search)) { die("Unable to search LDAP server"); } // lets sort the results by firstname if firstname is in the results foreach ($ldapSortAttributes as $eachSortAttribute) { if (in_array($eachSortAttribute, $ldapFilterAttributes)) { // making sure we don't accidentally try to sort against an inexisting field ldap_sort($this->connect, $search, $eachSortAttribute); } } // and here is the result $results = ldap_get_entries($this->connect, $search); ?> http://php.net/manual/en/function.ldap-sort.php

« previous php.notes (#167573) next »