Re: [PHP] LDAP sorting

2001-11-30 Thread Stig Venaas

The LDAP sorting code I posted a couple of days ago was a bit lacking.
Here's a function that should work:

function myldap_sort($data, $attr) {
// -1 because of the count entry
echo #, $n = count($data) - 1;
for ($i=0; $i$n; $i++) {
$a[$i]=$data[$i][$attr][0];
}
asort($a);
reset($a);
for ($j=0; list($i,) = each($a); $j++) {
$sorted[$j] = $data[$i];
}
return $sorted;
}

It's not fast, but should work. Example usage:
$info = ldap_get_entries($ds, $sr);
$sorted = myldap_sort($info, cn);

It assumes that the attribute specified is present in all the entries,
and only uses the first value of the attribute.

Stig

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] LDAP sorting

2001-11-28 Thread Stig Venaas

On Wed, Nov 28, 2001 at 03:08:52PM +0930, [EMAIL PROTECTED] wrote:
 Hi All-
 
 Can anyone tell me if PHP's LDAP functions support server side sorting of
 search results as defined in this RFC:
 
 http://www.ietf.org/rfc/rfc2891.txt

Not currently, maybe it will...  Depends on how many wants it.

 Sorting results from LDAP searches in PHP is a pain because of the
 structure
 of the result array.

Yes, but I think that's a poor excuse for doing it on the server side.
I think this should be done by the client in most cases.  One might
also need to take the clients locale into account when sorting the
data.

 Alternatively if anyone has a nice function for sorting the array returned
 by
 ldap_get_entries() by any desired attribute, that would be a great help
 also.

It's not that hard.  What I do, is that I create a new simple array
with just the attribute values I want to sort on and preserve the
indices of the result array.  Next I sort that array (again
preserving indices).  The indices of the sorted array now give me
the order I should use when displaying the entries from the result
array.  Not that hard, it looks something like this:

$n=ldap_count_entries($ds,$sr);
$info = ldap_get_entries($ds, $sr);
ldap_free_result($sr);

for ($i=0; $i$n; $i++) {
$cn[$i]=$info[$i][cn][0]);
}
asort($cn);
reset($cn);
for ($j=0; list($i,) = each($cn);) {
  $sorted[$j] = $info[$i];
}

Anyway, the next PHP release (in CVS or snapshots now) will have a
function ldap_sort() for client side sorting.  After you've done
the search, simply do ldap_sort($ds, $sr, cn);
and you have sorted the results on cn, and you access the result
as usual afterwards.

Stig

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] LDAP sorting

2001-11-27 Thread Adam . Whitehead

Hi All-

Can anyone tell me if PHP's LDAP functions support server side sorting of
search results as defined in this RFC:

http://www.ietf.org/rfc/rfc2891.txt

Sorting results from LDAP searches in PHP is a pain because of the
structure
of the result array.

Alternatively if anyone has a nice function for sorting the array returned
by
ldap_get_entries() by any desired attribute, that would be a great help
also.

Cheers.

Regards,
Adam Whitehead
Systems Developer - Computer Support and Maintenance
Ph. (08) 8936 3164 ** Mobile (0411) 241 120
E-mail: [EMAIL PROTECTED]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]