sambib Posted October 24, 2006 Share Posted October 24, 2006 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 successfulif($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 Quote Link to comment https://forums.phpfreaks.com/topic/24904-sorting-an-ldap-query-by-a-particular-element-in-the-returned-array/ Share on other sites More sharing options...
gmwebs Posted October 24, 2006 Share Posted October 24, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/24904-sorting-an-ldap-query-by-a-particular-element-in-the-returned-array/#findComment-113527 Share on other sites More sharing options...
sambib Posted October 24, 2006 Author Share Posted October 24, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/24904-sorting-an-ldap-query-by-a-particular-element-in-the-returned-array/#findComment-113958 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.