Dropdownlist Values From An Array

Hi all,

I was hoping someone can help me out with a small problem I have with dropDownList values?

I have already sorted out how to populate dropDownList values from related models but this is something I hope is a little simpler.

I have many dropDownList fields in my view files such as -




      <div class="control-group">

            <?php echo '<div class="control-label">Property Status</div>'; ?>

            <div class="controls">

                  <?php echo $form->dropDownList($model, 'pr_status', array('' => '', '12' => 'Active', '46' => 'Dead', '47' => 'Withdrawn', '48' => 'Included', '70' => 'Sold Prior', '72' => 'Sold Post', '73' => 'Sold On Day', '274' => 'Ongoing'), array()); ?>

            </div>

      </div> 

As I am starting to re-use those fields in other places I would like to pull the data in from a function in my model. I have this so far -


      public function getPropertyStatus() {


            if ($this->pr_status == 12)

                  $value = "Active";


            if ($this->pr_status == 46)

                  $value = "Dead";


            if ($this->pr_status == 47)

                  $value = "Withdrawn";


            if ($this->pr_status == 48)

                  $value = "Included";


            if ($this->pr_status == 70)

                  $value = "Sold Prior";


            if ($this->pr_status == 72)

                  $value = "Sold Post";


            if ($this->pr_status == 73)

                  $value = "Sold On Day";


            if ($this->pr_status == 274)

                  $value = "Ongoing";


            return $value;

      }

Does anyone know what code I would put into the dropDownList in the view form to use the data in the model function instead of defining each value in the array at the top of this post? As I mentioned earlier, this data is all in the same model so there are no relations.

Many thanks in advance.

Restructure your code like this in the model:




    private static $validPropertyStatuses = array(

        12 => 'Active',

        46 => 'Dead',

        ...

    );


    public static function getPropertyStatusOptions()

    {

        return self::$validPropertyStatuses;

    }


    public function getPropertyStatusForDisplay()

    {

        if (isset(self::$validPropertyStatuses[$this->pr_status]))

            return self::$validPropertyStatuses[$this->pr_status];


        return false; // Or throw exception or something

    }



Thanks Keith. That is an interesting way of doing it.

I have another question, what would need to go in the dropDownList field in the form view to pull in this data?

Many thanks




<?= $form->dropDownList($model, 'pr_status',

        ModelClass::getPropertyStatusOptions(),

        array('prompt'=>'')); ?>



Where ModelClass is the name of your model class.

Thats great. Thanks Keith. It works perfectly.

If I haven’t already overstayed my welcome in terms of your help could I please ask one more question about this issue?

How would I go about showing the array value of each pair (instead of the id) in a CgridView table?

I have this column currently in my grid -


        array(

            'name' => 'pr_status', // 

            'type' => 'raw',

            'value' => '$data->PropertyStatusOptions'

        ),

This just displays as ‘array’ in all rows? Ideally I would like to show it as ‘Active’ or ‘Dead’ etc.

Many thanks




        array(

            'name' => 'pr_status',

            'value' => '$data->getPropertyStatusForDisplay()',

        ),



Of course. Thanks for all of your help.