Jexx Posted August 26, 2009 Share Posted August 26, 2009 Hi I'm trying to 'echo' the output from my LDAP search so it shows in the order I want, not what the LDAP outputs. This is a clip of my code: $sector and $key are passed to the query.. $dn = "cn=Unit"; $filter="(&(objectclass=*)(|(|(|(id=$sector[$key])(id=$sector2[$key])(id=$sector3[$key])(id=$sector4[$key])))))"; $justthese = array('cn','id'); $sr=ldap_search($ds, $dn, $filter, $justthese); $info = ldap_get_entries($ds, $sr); for ($n=0; $n<$info['count']; $n++) { $usr = $info[$n]['cn'][0]; $id = $info[$n]['id'][0]; if ($id==$sector[$key]) $dist="Sector "; if ($id==$sector2[$key]) $dist="Sector 2 "; if ($id==$sector3[$key]) $dist="Sector 3"; if ($id==$sector4[$key]) $dist="Sector 4"; echo " " .$dist . " - " .$usr ."<br>"; } The output can show as: Sector - UserName Sector3 - UserName Sector4 - UserName Sector2 - UserName OR Sector - UserName Sector4- UserName Sector3 - UserName Sector2 - UserName Instead of : Sector - UserName Sector2 - UserName Sector3 - UserName Sector4 - UserName So how do I 'echo' my output to show in the order I choose ??? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171953-echo-output-in-selected-order/ Share on other sites More sharing options...
Jexx Posted August 27, 2009 Author Share Posted August 27, 2009 Any one any ideas on this ?? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171953-echo-output-in-selected-order/#findComment-907337 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 I don't think it's possible unless you know somehow to add a 'weight' to every record which you could use to sort 'em. Could you provide some more detailed examples instead of just sector - username. Their must be a clear reason why you want to sort in a particular order. Quote Link to comment https://forums.phpfreaks.com/topic/171953-echo-output-in-selected-order/#findComment-907343 Share on other sites More sharing options...
Jexx Posted August 27, 2009 Author Share Posted August 27, 2009 Hi Thanks for the reply. Obviously USERNAME changes depending on the username retrieved from the LDAP. Sector should read: (I'd shorted it for ease) Sector Location 14 by Davey bin - USERNAME Sector 7a Mint Press - USERNAME Sector 3 Slice Line - USERNAME Sector 9 Production - USERNAME Ideally I'd like these in the order as they appear on our internal paper work, NOT as they come from the LDAP. reading them into an array and then sorting them seems to be the answer.. but how !! ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171953-echo-output-in-selected-order/#findComment-907525 Share on other sites More sharing options...
Psycho Posted August 27, 2009 Share Posted August 27, 2009 Just take the results and dump them into a multidimensional array, then sort the array however you wish. THEN iterrate through the array to echo your results. Also, the arrays you are using for the sectors doesn't make sense. Is there any reason you just dont have one array for those in a format like this: $sectors = array ( 'sector1code' => 'Sector 1', 'sector2code' => 'Sector 2', 'sector3code' => 'Sector 3', 'sector4code' => 'Sector 4' ); Then you don't need all those IF statements. Instead you would have one line to set the $id value like this: $id = $sectors[$key]; Anyway, here is what you can do (although I would suggest a better array for the sector information): <?php $dn = "cn=Unit"; $filter = "(&(objectclass=*)(|(|(|(id=$sector[$key])(id=$sector2[$key])(id=$sector3[$key])(id=$sector4[$key])))))"; $justthese = array('cn','id'); $sr = ldap_search($ds, $dn, $filter, $justthese); $info = ldap_get_entries($ds, $sr); $ldap_results = array(); for ($n=0; $n<$info['count']; $n++) { $user = $info[$n]['cn'][0]; $id = $info[$n]['id'][0]; if ($sector==$sector[$key]) $dist="Sector "; if ($sector==$sector2[$key]) $dist="Sector 2 "; if ($sector==$sector3[$key]) $dist="Sector 3"; if ($sector==$sector4[$key]) $dist="Sector 4"; $ldap_results[] = array ( 'user' => $user, 'sector' => $$sector ); } function sortLDAPResults($a, $b) { if ($a['sector']==$b['sector']) { return 0; } return ($a < $b) ? -1 : 1; } //Sort the resutls usort($ldap_results, 'sortLDAPResults'); //Echo the results foreach ($ldap_results as $ldap_record) { echo " {$ldap_record['sector']} - {$ldap_record['user']}<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171953-echo-output-in-selected-order/#findComment-907542 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.