Jump to content


Photo

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


  • Please log in to reply
2 replies to this topic

#1 sambib

sambib
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 24 October 2006 - 08:23 AM

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).
?>
  <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>

any help greatly recieved....

cheers

#2 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 24 October 2006 - 09:28 AM

You may find this useful - http://www.developer...1114/page1.html

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

#3 sambib

sambib
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 24 October 2006 - 11:44 PM

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).


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 ?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users