Jump to content

Archived

This topic is now archived and is closed to further replies.

sambib

sorting an ldap query by a particular element in the returned array

Recommended Posts

Hi,

I'm pulling some data from an ldap query and want to sort (and display) the data by one of the returned elements in the array. This the part of code I'm struggling with. At the moment it's working fine except it's sorting by the email address (mail or $email) but I want to sort it by the surname (sn or $sn).
[code]?>
  <table id="ldap">
  <caption>Staff Contacts</caption>
    <tr>
  <th>Surname</th>
        <th>First Name</th>
        <th>E-Mail</th>
    </tr>
  <?php //Sort results if search was successful
if($result_array) {

  for($i=0; $i<count($result_array); $i++) {
    $format_array[$i][0] = $result_array[$i]["sn"][0];
    $format_array[$i][1] = $result_array[$i]["gn"][0];
    $format_array[$i][2] = $result_array[$i]["mail"][0];
    }

  //Sort array
  sort($format_array, "SORT_STRING");

  for($i=0; $i < count($format_array); $i++) {
    $sn = $format_array[$i][0];
    $gn = $format_array[$i][1];
    $email = $format_array[$i][2];

    if($sn && $gn && $email) {

echo "<tr><td class=\"name\">" . $sn . "</td><td class=\"name\">" . $gn . "</td><td class=\"email\"><a href=\"mailto:$email\">" . $email . "</a></td></tr>";
}
     
    }
  } else {
 
  echo "Result set empty for query: " . $ldap_query . "";
  }
  ?></table>[/code]

any help greatly recieved....

cheers

Share this post


Link to post
Share on other sites
You may find this useful - [url=http://www.developertutorials.com/tutorials/php/sorting-array-php-051114/page1.html]http://www.developertutorials.com/tutorials/php/sorting-array-php-051114/page1.html[/url]

And, as always, the PHP docs may be useful too - [url=http://uk.php.net/array]http://uk.php.net/array[/url]

Share this post


Link to post
Share on other sites
still looking for more help if anyone's able to......!

thanks for those, I've actually tried putting every availaible sort function sort(), asort(), ksort(), and krsort() with no luck. I've also tried taking out the "SORT_STRING" and nothing has changed the way it's sorted. I'll be honest and say that I don't even know how it's sorted now. It may be by one of the array elements that I'm not using (another ldap context attribute that I'm not actually using in the output).

The only thing that did work is when I tried shuffle() instead of sort (jsut as an experiment) and that did shuffle the results but I want them sorted by "sn" (which is the surname of the user).


[code]i

<?php if($result_array) {

  for($i=0; $i<count($result_array); $i++) {
    $format_array[$i][0] = $result_array[$i]["sn"][0];
    $format_array[$i][1] = $result_array[$i]["gn"][0];
    $format_array[$i][2] = $result_array[$i]["mail"][0];
    }

  //Sort array
sort($format_array, "SORT_STRING"); // *******this is where I'm struggling*******

  for($i=0; $i < count($format_array); $i++) {
    $sn = $format_array[$i][0];
    $gn = $format_array[$i][1];
    $email = $format_array[$i][2];

    if($sn && $gn && $email) {.......rest of cosde ?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.