2016-01-20 90 views
0

我有一个表(列,id,名称),而在名称中,我有几个月的名称(一月,二月...)我想采取所有这个名称,并将其返回到dropdownlist,我可以采取一个月。我在我的模型中有这个功能:如何从数据库表中获取数值作为数组并将其返回到下拉列表中?

public function getMonths() 
{ 
    $month= Months::find() 
->asArray() 
->all(); 

    for($monthNum = self::MY_START; $monthNum <= self::MY_MONTH; $monthNum++){ 
    return $month[$monthNum]; 
    } 

} 

现在它只返回我一个月(2月2日)。我应该怎么做才能在这个下拉列表中返回这些月份的名字?

+0

您使用的框架下拉列表...? – scaisEdge

+0

[使用数据库表中的值生成下拉列表输入]的可能副本(http://stackoverflow.com/questions/14221444/generate-drop-down-list-input-with-values-from-db-table) – miken32

回答

0

如果您正在使用Yii2,你可以简单地使用鉴于

<?php 
use yii\helpers\ArrayHelper; 
?> 

<?= Html::activeDropDownList($model, 'your_month_id', 
    ArrayHelper::map(Months::find()->all(), 'id', 'name')) ?> 
相关问题