How to create the custom Drop-down using CHtml::dropDownList

2 followers

Hi Friends, This tutorial may be help us create the custom drop-down menu using CHtml::dropDownList

1) first create/copy the drop-down menu in your _form.php file

<div class="control-group">
<label class="control-label"><?php echo $form->labelEx($model,'dob'); ?></label>
<div class="controls">
<?php  echo CHtml::dropDownList('VkUsers[month]', '', UtilityHtml::getMonthsArray());?>
<?php  echo CHtml::dropDownList('VkUsers[day]', '', UtilityHtml::getDaysArray());?>
<?php  echo CHtml::dropDownList('VkUsers[year]', '', UtilityHtml::getYearsArray());?>
</div>
</div>

2) Create the UtilityHtml.php file on componets folder which is include a all html file function

/**
     * @Method        : Dynemic Month
     * @Params        :
     * @author        : Ankit Modi
     * @created       : JULY 11 2014
     * @Modified by   :
     * @modified      :
     * @Comment       : Dynemic Month
     */
    public static function getMonthsArray()
    {
        for($monthNum = 1; $monthNum <= 12; $monthNum++){
            $months[$monthNum] = date('F', mktime(0, 0, 0, $monthNum, 1));
        }
 
        return array(0 => 'Month:') + $months;
    }
 
    /**
     * @Method        : Dynemic Days
     * @Params        :
     * @author        : Ankit Modi
     * @created       : JULY 11 2014
     * @Modified by   :
     * @modified      :
     * @Comment       : Dynemic Days
     */
    public static function getDaysArray()
    {
        for($dayNum = 1; $dayNum <= 31; $dayNum++){
            $days[$dayNum] = $dayNum;
        }
 
        return array(0 => 'Day:') + $days;
    }
 
    /**
     * @Method        : Dynemic Years
     * @Params        :
     * @author        : Ankit Modi
     * @created       : JULY 11 2014
     * @Modified by   :
     * @modified      :
     * @Comment       : Dynemic Years
     */
    public static function getYearsArray()
    {
        $thisYear = date('Y', time());
 
        for($yearNum = $thisYear; $yearNum >= 1920; $yearNum--){
            $years[$yearNum] = $yearNum;
        }
 
        return array(0 => 'Year:') + $years;
    }

Total 1 comment

#17689 report it
Rohit Suthar at 2014/07/15 02:01am
Better to use component

Better to use component -

e.g.

class UtilityHtml extends CApplicationComponent {
 
 public function getMonthsArray() {
 
     //your code here
     //your code here
 
     return value;
 }
 
 
 public function getDaysArray() {
 
     //your code here
     //your code here
 
     return value;
 }
 
}

Need to set it up in config/main.php:

'components' => array(
        'myUtils' => array(
            'class' => 'ext.components.UtilityHtml',
        ),

So now you can use like this:

<?php  echo CHtml::dropDownList('VkUsers[month]', '', Yii::app()->UtilityHtml->getMonthsArray());?>

According to this - http://www.yiiframework.com/wiki/727/updated-how-to-create-call-custom-global-function-in-whole-application/#c17607

Leave a comment

Please to leave your comment.

Write new article