Issue: Always Last Item Is Showed In Drop Down List

Hi,

I added a drop down list. But it always showed the last item. I want to show first item always.

In controller,




$shippingMethod = ShippingMethod::model()->findAll();



In view




<?php $list = CHtml::listData($shippingMethod, 'id', 'shipping_method'); ?>

<?php echo CHtml::dropDownList('shipping_method', $shippingMethod, $list, array('class' => 'input_lg length_lg')); ?>



This is how it shows with firebug.




<select id="shipping_method" name="shipping_method" class="input_lg length_lg valid">

  <option selected="selected" value="1">UPS</option>

  <option selected="selected" value="2">FedEx</option>

  <option selected="selected" value="3">DHL</option>

  <option selected="selected" value="4">GSO</option>

  <option selected="selected" value="5">USPS</option>

  <option selected="selected" value="6">Other</option>

</select>



Any help would be highly appreciated.

You have an error here:




1. $shippingMethod = ShippingMethod::model()->findAll();

2. $list = CHtml::listData($shippingMethod, 'id', 'shipping_method'); 

3. echo CHtml::dropDownList('shipping_method', $shippingMethod, $list, array('class' => 'input_lg length_lg')); 



Infact $shippingMethod is at line 1 an array. Then in line 2 you create correctly $list (html listdata), but at last you are using again $shippingMethod (that is an array) as selected value (field n.2 of dropDownList method).

So your selcted value is wrong!

Thanks you for pointing out the error. I was able to fix it.

<?php echo CHtml::dropDownList(‘shipping_method’, ‘’, $list, array(‘class’ => ‘input_lg length_lg’)); ?>