|
Posted by benb on 01/30/07 10:31
"Colin McKinnon"
<colin.thisisnotmysurname@ntlworld.deletemeunlessURaBot.com> wrote in
message news:Yotvh.85063$z01.32348@newsfe3-gui.ntli.net...
> benb wrote:
>
>> <snip>
>
> You'll need to write your own comparison function (not tested - YMMV):
>
>
> function my_sort(&$a, &$b)
> {
> global $sort_by;
> if ($a[$sort_by][0]==$b[$sort_by][0]) return 0;
> return ($a[$sort_by][0]<$b[$sort_by][0]) ? -1 : 1;
> }
>
> $sort_by = 'displayname';
> usort($info, 'my_sort');
> // or if you prefer...
> $sort_by = 'mobile';
> usort($info, 'my_sort');
>
>
>
> Note that you don't have to worry about iterating through th array
> yourself,
> or having to code your own quicksort when you discover how slow a bubble
> sort really is...etc.
>
> HTH
>
> C.
Hi Colin,
I tried that code, and it works perfectly in a plain php file. However I'm
trying to add this to a wiki (dokuwiki) so that people can use it to look up
contact info for other staff members. When I c&p the code into the wiki, and
try and run it, the sort function doesn't work. I can't see any reason for
this not to work, I was wondering if you knew of any reason? I have posted
my entire code below, so you can see how it interacts with Active Directory.
I've enable PHP in the wiki config, and it is running the code, and
displaying the records, just not sorting them!
$ldap_host = "ldap://server.domain.local";
$ldap_port = "389";
$base_dn = "OU=Mobile,DC=domain,DC=local";
$filter = "(&(objectClass=user)(objectCategory=person)(CN=*))";
$ldap_user = "CN=Admin,OU=hq,DC=domain,DC=local";
$ldap_pass = "password";
$inforequired = array("displayname","mobile");
$connect = ldap_connect($ldap_host,$ldap_port)
or exit("Could not connect to LDAP server");
// required to search AD, according to note in PHP manual notes
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
$bind = ldap_bind($connect, $ldap_user, $ldap_pass)
or exit("Could not bind to $ldap_host");
//echo "Successful bind to $ldap_host with $bind<br><br>\n";
$read = ldap_search($connect, $base_dn, $filter, $inforequired)
or exit("Unable to search ldap server");
$info = ldap_get_entries($connect, $read);
//echo $info["count"]." entries returned for $filter<br><br>\n";
function my_sort(&$a, &$b)
{
global $sort_by;
if ($a[$sort_by][0]==$b[$sort_by][0]) return 0;
return ($a[$sort_by][0]<$b[$sort_by][0]) ? -1 : 1;
}
$sort_by = 'displayname';
usort($info, 'my_sort');
if($info == 0)
{
print "<p>No information available";
}
else
{
print "<table><tr>"
. "<td><b>Name</b></td>"
. "<td><b>Mobile Number</b></td>"
. "</tr>";
for($i=1;$i<$info[0];$i++)
{
print "<tr><td>" . $info[$i]["displayname"][0] . "</td>";
if (isset($info[$i]["mobile"][0]))
{
print "<td>" . $info[$i]["mobile"][0] . "</td></tr>";
} else {
print "<td>No Number Available</td></tr>";
}
}
print "</table>";
}
ldap_unbind($connect);
[Back to original message]
|