Jump to content

echo output in selected order.


Jexx

Recommended Posts

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

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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>";              
   }

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.