dropDownList > dropDownList > findAll > order

Ok here goes

I cant seem to get this sort order working, but from everything i’ve read, it should be working! ???

The database has a table with form field contents in, so for example to select age in a drop down, it finds all the rows with an id_field value of 2, and puts them in the drop down, great.

id id_field value

1 2 18

2 2 19

3 2 20

4 2 21

This is the dropdown code


<?php echo $form->dropDownList($model,'client_2_age', CHtml::dropDownList(FormFields::model()->findAll("id_field=2",array('order' => 'value ASC')), 'value', 'value'), array('empty'=>'Select age')) ?>



I thought it was working, until someone ‘kindly’ pointed out i hadnt included the age “58” so i added it to to DB, and sure enough it appeared in the dropdown, but as the last entry. :-X

I cant get it to position itself between 57 and 59! >:(

I obviously entered them into the DB in the correct order, so thought it was working, doh! :o

Any help appreciated.

ps sorry about the icons, they are keeping me ‘up beat’ lol

anyone?

It seems strange that its not sorting… it’s showing Value ok, but not sorting by Value




<?php echo $form->dropDownList($model,'client_1_age', CHtml::listData(FormFields::model()->findAll("id_field=2",array('order' => 'value ASC')), 'value', 'value'), array('empty'=>'Select age')) ?>

:(

I think it should be


<?php echo $form->dropDownList($model,'client_1_age', CHtml::listData(FormFields::model()->findAll(array('condition'=>'id_field=2', 'order' => 'value ASC')), 'value', 'value'), array('empty'=>'Select age')) ?>

Does it help?

PS But of course you can write, if no order needed:


<?php echo $form->dropDownList($model,'client_1_age', CHtml::listData(FormFields::model()->findAll('id_field=2'), 'value', 'value'), array('empty'=>'Select age')) ?>

YES!!! that works, thank you, i didnt know about this condition part, i will now go and fix alot of dropdowns…

One thing thats not 100% perfect, is that in a list of 18 to 100… 100 is at the top (I know that logically 10x is higher than 18)

How can I solve that? perhaps i change it to 18 to 99 lol

Glad it worked. Now, are you sure your data field is of some numeric type? It looks like it’s a string type to behave like that.

Do you mean in the database? it’s set to a Varchar because it holds data from any type of form element, for example MR, MRS, MISS etc.

not sure how to set a data type in Yii yet… I will research

Yes, in the db. I’m talking about your ‘value’ column. It should be numeric (TINYINT unsigned is enough for ages if you use MySQL).

Now, do you mean you store ages AND titles/civilities in the same column?

uhm, yes hides behind pillow dont hurt me!

I just wanted to store all the "pre poulated" field values somewhere, so that I can change them once, and all forms get updated - also at some point, allow Admin to set them via the GUi…

MySQL yep. Perhaps then I should create two columns, one varchar, the other int…

I’m not an angel but I generally don’t hurt anyone. I was just surprised :)

I understand what you want to do, but you’ve already experienced one downside of it for sorting.

And if I were you, UX-wise, I wouldn’t really use a dropdown for something like age. I’d rather use a slider. And do you really need to have age values stored in database?

Anyway, I’m really just saying out loud what I think. Feel free to do as you need.

PS I’ve just realized we are in the Yii 1.0.x subforum… Is it on purpose?

Hi,

I’ve created two columns now, value_int and value_char this has solved the ordering problem. thanks for steering me there.

I’d quite like to use a slider - but for now, im building a quick and dirty application to solve a problem - but that can be built upon going forwards - with things like sliders! actually, i did have a slider ;D for £House Value, but ran into difficulties, so bailed until I have time to go back and make it work…

Oh, didnt realise i was in the Yii 1.0.x forum, opps.

p

thanks bennouna

i was looking to order the dropdownlist by username ASC. Your code really helped me.


<?php echo $form->dropDownList($model,'client_1_age', CHtml::listData(FormFields::model()->findAll(array('condition'=>'id_field=2', 'order' => 'value ASC')), 'value', 'value'), array('empty'=>'Select age')) ?>



Bennouna is a great guy isnt he - even if he is now hiding behind a logo! :lol: