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

You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#3) »

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

1) first create 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;
	}