Author Topic: Alphabetically sort a drop down list  (Read 278 times)

Miles

  • Guest
Alphabetically sort a drop down list
« on: May 15, 2012, 02:37:05 PM »
Code: [Select]
$managers = $this->Job->Manager->find( 'list');

When I display my job managers doing it this way will do it in alphabetical order. I needed to add a condition though.

Code: [Select]
$managers = $this->Job->Manager->find( 'list', array('conditions' => array('enabled' => 1)));

I needed to add the condition so it was only retrieving job managers whose accounts were enabled and after that it organized the list in order of their primary key.

Is there any way to have this condition while still keeping the list in alphabetic order to make it easy to find names?

RaythXC

  • PHP Programmer & Web-Designer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 395
  • Karma: 10
  • Freelance PHP Programmer/Web-Designer
    • View Profile
    • Rayth.Info
Re: Alphabetically sort a drop down list
« Reply #1 on: May 15, 2012, 04:35:13 PM »
Without seeing your full code it will be hard to see exactly what you are doing. I would imagine that your using MySQL in which case just order your mysql query.
RaythXC - My Home Site
Note: most answers I give come from the php manual located at PHP.Net

Ojoshiro

  • Technocentror
  • Senior Member
  • ****
  • Posts: 150
  • Karma: 4
  • That is not dead which can eternal loop...
    • View Profile
Re: Alphabetically sort a drop down list
« Reply #2 on: May 16, 2012, 08:40:28 AM »
I assume that somewhere after this code you actually generate a dropdown list.
What do you do that with?

If you first make an hash, sorry, associatve array of the names and the job/manager/thingies.
Somewhere in the objects you retrieve there is no doubt some getter-function with which you can get the name in the object.
PHP Code: [Select]

$assarr 
= array();
foreach ( 
$managers as $manager )
{   
$assarr[$manager->getName()] = $manager;
}

and then you  sort the associative array by its keys ( the names )
PHP Code: [Select]

ksort
$assarr );

after that you use your associative arrays order to show the managers/job/thingies in the right order
No idea how you do that, but let's say you do with method 'render':
PHP Code: [Select]

foreach ( $asarr as $name$object )
{   
$object->render();
}


Something like this?

O.
T.A.N.S.T.A.A.F.L.
_______________________________/