I have managed to get information from our MS Exchange 5.5 server using LDAP.
The only problem is that the results don't sort well.
If i only search on 1 field then thats OK, but i search multiple fields it doesn't order well at all.

Any ideas how i might sort them??
(i have seen the LDAP_Sort function, however, i run PHP4.1.2 and the manual entry was very vague indeed)

This is the code I'm using to retrieve the info.

$retrieved_fields = array ( "givenname", "sn", "title", "department", "telephonenumber");
@$sr=ldap_search($ds,"o=Benenden Healthcare Society, c=US", $search_terms, $retrieved_fields);
$matches = ldap_count_entries($ds,$sr);
$info = ldap_get_entries($ds, $sr);
for ($i = 0; $i < $info["count"]; $i++)
        {
?>
        <tr>
          <td class="searchcontent">&nbsp;<?=$info[$i]["givenname"][0]?>&nbsp;</td>
          <td class="searchcontent">&nbsp;<?=$info[$i]["sn"][0]?>&nbsp;</td>
          <td class="searchcontent">&nbsp;<?=$info[$i]["title"][0]?>&nbsp;</td>
          <td class="searchcontent">&nbsp;<?=$info[$i]["department"][0]?>&nbsp;</td>
          <td class="searchcontent">&nbsp;<?=$info[$i]["telephonenumber"][0]?>&nbsp;</td>
        </tr>
        <?
        }

TIA

Jamie

    Have you looked at array_multi_sort?

      Thanks for the tip,

      I've had a look through the php.net entry for array_multisort. I'm not sure i quite understand how it would work with an LDAP result as the arrays are $array[$search_row][fieldname] rather than $array[fieldname][$search_row]

      Cheers

      Jamie

        You're right. This looks more like a job for usort / uasort.

          I've abandonned the item of sorting arrays retrieved from LDAP, instead i have created my own arrays, 1 for each field, and then sorted them, so i now have,

                  for ($i = 0; $i < $info["count"]; $i++)
                  {
          			$givenname_array[$i] = $info[$i]["givenname"][0];
          			$surname_array[$i] = $info[$i]["sn"][0];
          			$title_array[$i] = $info[$i]["title"][0];
          			$department_array[$i] = $info[$i]["department"][0];
          			$telephonenumber_array[$i] = $info[$i]["telephonenumber"][0];
          			$dn_array[$i] = $info[$i]["dn"];
          		}
          		asort($surname_array); //this should alphabetically sort the surname field and keep the indexes intact
          		foreach($surname_array as $key=>$value)
          		{
          			//display the output, using $key as the array indexes
          			?>
                  <tr>
                    <td class="searchcontent"><?=$givenname_array[$key]?>&nbsp;</td>
                    <td class="searchcontent"><?=$surname_array[$key]?>&nbsp;</td>
                    <td class="searchcontent"><?=$title_array[$key]?>&nbsp;</td>
                    <td class="searchcontent"><?=$department_array[$key]?>&nbsp;</td>
                    <td class="searchcontent"><?=$telephonenumber_array[$key]?>&nbsp;</td>
                    <td class="searchcontent"><a href="ldap_more_info.php?dn=<?=urlencode($dn_array[$key])?>">More Info</a>&nbsp;</td>
                  </tr>
                  <?
                  }
          

          This seems to work great now. (ordered by surname A-Z)

          Cheers for the help.

          Jamie

            2 years later

            i was wondering if someone could help me with my code. I am trying to sort my ldap results but dont know how to do it from the code i already have. If any one has pointers, post 'em and i will be forever greatful!

            Current code:

            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <title>Search</title>
            <style type="text/css">
            <!--
            body,td,th {
            	font-family: Arial;
            	font-size: 12px;
            	color: #FFFFFF;
            }
            body {
            	background-color: #505A7E;
            	margin-left: 10px;
            	margin-top: 0px;
            	margin-right: 0px;
            	margin-bottom: 0px;
            }
            a:link {
            	color: #FFFFFF;
            	text-decoration: none;
            }
            a:visited {
            	text-decoration: none;
            	color: #FFFFFF;
            }
            a:hover {
            	text-decoration: underline;
            	color: #FF9900;
            }
            a:active {
            	text-decoration: none;
            }
            -->
            </style>
            </head>
            <body>
            <form action="process.php" method="post">
              <p>Search criteria:<br />
                <input type="text" name="keyword" size="25"
                       maxlength="20" value="" />
                <br>
                Filter:<br>
                <select name="filter">
                    <option value="">Choose One:</option>
                    <option value="sn">Last Name</option>
                    <option value="telephonenumber">Phone (###-###-####)</option>
                    <option value="department">Department</option>
                </select>
                <br>    
            <input type="submit" value="Search!" /> <br> <a href="instructions.html" target="_blank">How to use this search! </a> </p> </form> <center> <?php // Designate a few variables $host = "ldap://server.com"; $user = "userid@server.com"; $pswd = "password"; $ad = ldap_connect($host) or die( "Could not connect!" ); // Set version number ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3) or die ("Could not set ldap protocol"); // Binding to ldap server $bd = ldap_bind($ad, $user, $pswd) or die ("Could not bind"); // Create the DN $dn = "OU=Domain Users,DC=server,DC=com"; // Specify only those parameters we're interested in displaying $attrs = array("displayname","department","physicaldeliveryofficename","mail","telephonenumber","facsimiletelephonenumber","wwwhomepage"); // Create the filter from the search parameters $filter = $_POST['filter']."=".$_POST['keyword']."*"; $search = ldap_search($ad, $dn, $filter, $attrs) or die ("ldap search failed"); $entries = ldap_get_entries($ad, $search); if ($entries["count"] > 0) { for ($i=0; $i<$entries["count"]; $i++) { echo "<table border=0 cellpadding=3 bgcolor=#003366><tr><td><b>Name: </b></td><td>".$entries[$i]["displayname"][0]."</td>"; echo "</tr><td><b>Department: </b></td><td>".$entries[$i]["department"][0]."</td><br />"; echo "</tr><td><b>Office: </b></td><td>".$entries[$i]["physicaldeliveryofficename"][0]."</td><br />"; echo "</tr><td><b>Phone: </b></td><td>".$entries[$i]["telephonenumber"][0]."</td><br />"; echo "</tr><td><b>Fax: </b></td><td>".$entries[$i]["facsimiletelephonenumber"][0]."</td><br />"; echo "</tr><td><b>Email: <b></td><td><a href=mailto:".$entries[$i]["mail"][0].">".$entries[$i]["mail"][0]."</a></td>"; echo "</tr><td><b>Picture: </b></td><td><img src=".$entries[$i]["wwwhomepage"][0]." border=2 style=border-color:#FFFFFF></td></tr></table>"; echo "Record #".$i."<br>--------"; } } else { echo "No results found!"; } ldap_unbind($ad); ?> </center> </body> </html>

            thanks a bunch!

              Write a Reply...