note 85317 added to function.ldap-sort

From: Date: Tue, 26 Aug 2008 04:40:51 +0000
Subject: note 85317 added to function.ldap-sort
Groups: php.notes 
Request: Send a blank email to php-notes+get-143816@lists.php.net to get a copy of this message
I was very unhappy because doesn't exists a way to order ldap search results. Above a solution to order a search in ascending and descending order. This function also do pagination. // descending order $list = ldap_sort_paginate( $conn, $search, "cn", "desc" ); // ascending order with pagination $list = ldap_sort_paginate( $conn, $search, "cn", "asc", 3, 15 ); Use http://php.net/ldap_count_entries to calculate the min and max pages values before call this function. The first page value is 0 (zero). <?php /** * Order a search in ascending and descending order. * * @param resource from ldap_connect() * @param resource from ldap_search() * @param string of attribute to order * @param string "asc" or "desc" * @param integer page number, first is 0 zero * @param integer entries per page * @return string[] */ function ldap_sort_paginate( $rConnection, $rSearch, $sField, $sOrder = "asc", $iPage = null, $iPerPage = null ) { $iTotalEntries = ldap_count_entries( $rConnection, $rSearch ); if ( $iPage === null || $iPerPage === null ) { # fetch all in one page $iStart = 0; $iEnd = $iTotalEntries - 1; } else { # calculate range of page $iStart = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage; $iEnd = $iStart + $iPerPage - 1; if ( $sOrder === "desc" ) { # revert range $iStart = $iTotalEntries - 1 - $iEnd; $iEnd = $iStart + $iPerPage - 1; } } var_dump( $iStart . " " . $iEnd ); # fetch entries ldap_sort( $rConnection, $rSearch, $sField ); $aList = array(); for ( $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch ); $iCurrent <= $iEnd && is_resource( $rEntry ); $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry ) ) { if ( $iCurrent >= $iStart ) { array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ) ); } } # if order is desc revert page's entries return $sOrder === "desc" ? array_reverse( $aList ) : $aList; } ---- Server IP: 200.185.126.110 Probable Submitter: 200.103.118.38 ---- Manual Page -- http://www.php.net/manual/en/function.ldap-sort.php Edit -- https://master.php.net/note/edit/85317 Del: integrated -- https://master.php.net/note/delete/85317/integrated Del: useless -- https://master.php.net/note/delete/85317/useless Del: bad code -- https://master.php.net/note/delete/85317/bad+code Del: spam -- https://master.php.net/note/delete/85317/spam Del: non-english -- https://master.php.net/note/delete/85317/non-english Del: in docs -- https://master.php.net/note/delete/85317/in+docs Del: other reasons-- https://master.php.net/note/delete/85317 Reject -- https://master.php.net/note/reject/85317 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143816) next »