Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : LDAP Functions : ldap_sort

ldap_sort

Sort LDAP result entries (PHP 4 >= 4.2.0, PHP 5)
bool ldap_sort ( resource link, resource result, string sortfilter )


Code Examples / Notes » ldap_sort

tarnishednitenospa t mgmail

While bob's sort is nice I found that this one was a little more to my liking. This little library ignores case and sorts on multiple criteria with some fairly short and simple code. I'm sure it could be simplified further - switch statement perhaps.
<?php
/*Scoped global*/
$LDAPSortOrder;
/*$array is the return from ldap_get_entries
  $sortBy is an array of values to sort by in the order in                    
  which to sort them
  $reverse will sort in reverse order if true
*/
function LDAPSort(&$array,$sortBy,$reverse=FALSE){
       global $LDAPSortOrder;
       $LDAPSortOrder = $sortBy;
       if($reverse){
               usort($array,'ldapcomparereverse');
       } else {
               usort($array,'ldapcompare');
       }
}
/* The compare functions always check case insensitive*/
function ldapcompare($x,$y){
       global $LDAPSortOrder;
/*
Loops through the items in order until a difference is found
*/
       foreach($LDAPSortOrder as $ele){
               if(strtolower($x[$ele][0]) == strtolower($y[$ele][0]))
                       continue;
               elseif(strtolower($x[$ele][0]) < strtolower($y[$ele][0]))
                       return -1;
               else
                       return 1;
       }
       return 0;
}
/*Just like above but in reverse order*/
function ldapcomparereverse($x,$y){
       global $LDAPSortOrder;
       foreach($LDAPSortOrder as $ele){
               if(strtolower($x[$ele][0]) == strtolower($y[$ele][0]))
                       continue;
               elseif(strtolower($x[$ele][0]) < strtolower($y[$ele][0]))
                       return 1;
               else
                       return -1;
       }
       return 0;
}
?>
JML


zbaizman

This note may be self-evident, but the functionality of ldap_sort threw off this sometime user of relational databases.
The following code will NOT do what you expect:
<?php
// omitted calls to connect and and bind to LDAP server...
// attributes we want to retrieve from LDAP server
$ldap_attributes = array ( 'cn', 'o', 'mail' ) ;
// retrieve attributes from matching entries
$search_results = ldap_search ( $ldap_conn, 'dc=example,dc=org', '(objectclass=*)', 0, 500, 30 ) ;
// sort entries by last name ('sn')
ldap_sort ( $ldap_conn, $search_results, 'sn' ) ;
?>
The entries will NOT be sorted by last name. Why not? Because LDAP doesn't function like a RDBMS; you cannot sort a result set on an arbitrary field, regardless of whether you "selected" it. You must always include the attribute by which you want to sort your entries among the requested attributes (add 'sn' to $ldap_attributes, in this case).
Hope this is helpful to some other folks who scratched their heads when they tried to sort entries and it didn't work out...


matthew dot j dot gray

This function applies strcmp() to each attribute (given by sortfilter) in order to sort the entries returned by the server.  To order search results ascending by last name, try passing "sn" as the sortfilter argument.  This function does not play nice with multi-valued attributes.

rieflin

The note from Sean on 14-Nov-2002 was of great help to me but I had to change one line for it to work.
In the example below, change the line ...
ldap_sort = ($ldap_connection,$search_return,"givenname");
to ldap_sort ($ldap_connection,$search_return,"givenname");
and everything should work fine.  
Sean's code.................
The following is a working example of how to use the ldap_sort function ...
$search_return = ldap_search($ldap_connection,$ldap_base,$search_filter);
ldap_sort = ($ldap_connection,$search_return,"givenname");
$entries = ldap_get_entries($ldap_connection,$search_return);
This will take the returned search sort it by the "givenname" attribute. It has to be done BEFORE you call ldap_get_entries.


sean dot ogrady

The following is a working example of how to use the ldap_sort function ...
$search_return = ldap_search($ldap_connection,$ldap_base,$search_filter);
ldap_sort = ($ldap_connection,$search_return,"givenname");
$entries = ldap_get_entries($ldap_connection,$search_return);
This will take the returned search sort it by the "givenname" attribute. It has to be done BEFORE you call ldap_get_entries.


jason dot sokolowski

Something real simple i wrote to sort directory searches by a persons last name.
for($i=0;$i<$result["count"];$i++)
{
$lastname = $result[$i]["sn"][0];
$lnames["$i"]=$lastname;
}//for i
@asort($lnames);


ccblanket

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);
?>


helmut dot patay

If you want to sort the search results based on any attribute reverse,
please do not use the examples above containing two loops!
You will end up in changing php.ini with increasing the value
of 'max_execution_time' dramatically. No one will use your website
again!
I have used the following construct and it works very well:
// write this function
function myReverseCnCmp( $a, $b ) {
   // Note: $b comes before $a because of reverse ordering
   return strcasecmp( $b[cn][0], $a[cn][0] );
}
// search
$sr = ldap_search( $ds, $baseDn, $filter );
// before reading the entries, you must sort normally
// in my example I use the attribute 'cn' for sorting
// instead of cn you can use any other attribute
ldap_sort( $ds, $sr, "cn" )
// read the entries
$entries = ldap_get_entries( $ds, $sr );
// now sort the array reverse with usort
usort( $entries, "myReverseCnCmp" );
// now $entries is sorted reverse based on "cn"
// This runs at least 5 times faster than the double loop
// solutions
// Note: If you want to sort reverse based on dynamic
// attribute names, you must do some if's or switch'es,
// but it's really fast!


ben

If you are wanting to sort by multiple attributes, for instance ordering by last name and then first name,  try this function. This is similar to "ORDER BY lastname, firstname"  in SQL.
This function uses an insertion sort algorithm, which is somewhat faster then the old-fashoned bubble sort.  The second argument is an array containing the attributes you want to sort by. (this functon won't do descending or ascending..  feel free to add it!)
<?php
/**
* @param array $entries
* @param array $attribs
* @desc Sort LDAP result entries by multiple attributes.
*/
function ldap_multi_sort(&$entries, $attribs){
for ($i=1; $i<$entries['count']; $i++){
$index = $entries[$i];
$j=$i;
do {
//create comparison variables from attributes:
$a = $b = null;
foreach($attribs as $attrib){
$a .= $entries[$j-1][$attrib][0];
$b .= $index[$attrib][0];
}
// do the comparison
if ($a > $b){
$is_greater = true;
$entries[$j] = $entries[$j-1];
$j = $j-1;
}else{
$is_greater = false;
}
} while ($j>0 && $is_greater);

$entries[$j] = $index;
}
return $entries;
}
?>


nick

I'm not sure if there's a way to make ldap_sort case insensitive, so I'm sticking with Ben's solution with the following amendment.
<?php
...
$a .= strtolower($entries[$j-1][$attrib][0]);
$b .= strtolower($index[$attrib][0]);
...
?>
Hope it's useful.


askgopal

Here's a simple LDAP sort function I wrote:
function sort_ldap_entries($e, $fld, $order)
{
for ($i = 0; $i < $e['count']; $i++) {
for ($j = $i; $j < $e['count']; $j++) {
$d = strcasecmp($e[$i][$fld][0], $e[$j][$fld][0]);
switch ($order) {
case 'A':
if ($d > 0)
swap($e, $i, $j);
break;
case 'D':
if ($d < 0)
swap($e, $i, $j);
break;
}
}
}
return ($e);
}
function swap(&$ary, $i, $j)
{
$temp = $ary[$i];
$ary[$i] = $ary[$j];
$ary[$j] = $temp;
}
so that it can be invoked like:
   $entries = sort_ldap_entries($entries, 'mail', 'A'); // sort entries by ascending order of mail
where,
   `$entries' is the array returned by ldap_get_entries() function.
This might be useful to those who still run older versions of PHP (<= 4.2.0) on their web servers :-)


john

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 "
<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!


huntz

About the note of ccblanket at yahoo dot com...
Nice tip, but on line 2 of your example we need to use $ldapSortAttributes and not $ldapSortAttribues :-)
So, the whole correct function is:
<?php
  $ldapFilterAttributes = array ('givenname', 'sn', 'mail');
  $ldapSortAttributes = array('sn', 'givenname');
 
  $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");
  }
  foreach ($ldapSortAttributes as $eachSortAttribute) {
      if (in_array($eachSortAttribute, $ldapFilterAttributes)) {
          ldap_sort($this->connect, $search, $eachSortAttribute);
      }
  }
  $results = ldap_get_entries($this->connect, $search);
print_r($results);
?>


Change Language


Follow Navioo On Twitter
ldap_8859_to_t61
ldap_add
ldap_bind
ldap_close
ldap_compare
ldap_connect
ldap_count_entries
ldap_delete
ldap_dn2ufn
ldap_err2str
ldap_errno
ldap_error
ldap_explode_dn
ldap_first_attribute
ldap_first_entry
ldap_first_reference
ldap_free_result
ldap_get_attributes
ldap_get_dn
ldap_get_entries
ldap_get_option
ldap_get_values_len
ldap_get_values
ldap_list
ldap_mod_add
ldap_mod_del
ldap_mod_replace
ldap_modify
ldap_next_attribute
ldap_next_entry
ldap_next_reference
ldap_parse_reference
ldap_parse_result
ldap_read
ldap_rename
ldap_sasl_bind
ldap_search
ldap_set_option
ldap_set_rebind_proc
ldap_sort
ldap_start_tls
ldap_t61_to_8859
ldap_unbind
eXTReMe Tracker