Hi,
I'm using the following code in our internal wiki to show staff contact numbers. However, for some reason its displays all the staff, minus the last person in the query (alphabetically). i.e. If the query returns bob, fred, mark & terry, it won't display terry, as he's last in the query, if I create a new user called Zak, it'll display Terry, but not Zak.
$ldap_host = "ldap://server.domain.com";
$ldap_port = "389";
$base_dn = "OU=office,DC=domain,DC=com";
$filter = "(memberof=CN=AllStaff,OU=Distribution Groups,OU=office,DC=domain,DC=com)";
$ldap_user = "CN=Query,OU=Infrastructure,OU=office,DC=domain,DC=com";
$ldap_pass = "********";
$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;
$sort_by = 'displayname';
if ($a[$sort_by][0] == $b[$sort_by][0]) return 0;
return ($a[$sort_by][0]<$b[$sort_by][0])? -1 : 1;
}
usort($info, 'my_sort');
function genericCmp($a, $b) {
if ($a == $b) {
return 0;
} elseif ($a < $b) {
return -1;
} else {
return 1;
}
}
$sort_by = 'displayname';
usort($info, create_function('$a,$b', "return genericCmp(\$a['$sort_by'][0], \$b['$sort_by'][0]);"));
if($info == 0)
{
print "<p>No information available";
}
else
{
print "<div align=\"center\"><table width=\"30%\" class=\"inline\"><tr>"
. "<th><b>Name</b></th>"
. "<th><b>Mobile Number</b></th>"
. "</tr>";
for($i=1;$i<$info[0];$i++)
{
print "<tr><td align=\"left\">" . $info[$i]["displayname"][0] . "</td>";
if (isset($info[$i]["mobile"][0]))
{
print "<td align=\"left\">" . $info[$i]["mobile"][0] . "</td></tr>";
} else {
print "<td align=\"left\">No Number Available</td></tr>";
}
}
print "</table></div>";
}
ldap_unbind($connect);
The code is a bit of a mish mash from other codes snipits that I've put together, but I can't see whats wrong with it, I think its something to do with the sort function, but I'm not to sure.
Any help would be much appreciated.
Ben