note 41472 deleted from function.ldap-sort by danbrown

From: Date: Wed, 31 Mar 2010 16:23:16 +0000
Subject: note 41472 deleted from function.ldap-sort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167575@lists.php.net to get a copy of this message
Note Submitter: john at benden dot co dot uk ---- 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!

« previous php.notes (#167575) next »