Yii/PHP, 年份下拉列表
Yii/PHP, year dropdownlist
所以,基本上我有这段代码:
<?php echo '<strong>Y: </strong>'.$form->dropDownList($model, 'year', array('2015'=>'2015','2014'=>'2014')); ?>
如您所见,我有一种非常手动的方式来添加年份。有没有办法让我每年自动添加它?或者也许是一年的范围?
如有任何建议,我们将不胜感激。谢谢。
<?php echo '<strong>Y: </strong>'.$form->dropDownList($model, 'year', array(date("Y")=>date("Y"),date("Y")-1=>date("Y")-1,date("Y")-2=>date("Y")-2,date("Y")-3=>date("Y")-3 )); ?>
我知道这还不是完全自动化的。但它仍然每年更新自己。
在您的模型中添加此功能
public function getYearsList() {
$currentYear = date('Y');
$yearFrom = 2013;
$yearsRange = range($yearFrom, $currentYear);
return array_combine($yearsRange, $yearsRange);
}
在你的表格中这样称呼它:
$form->dropDownList($model, 'year', $model->getYearsList());
了解 php 的 range 函数
所以,基本上我有这段代码:
<?php echo '<strong>Y: </strong>'.$form->dropDownList($model, 'year', array('2015'=>'2015','2014'=>'2014')); ?>
如您所见,我有一种非常手动的方式来添加年份。有没有办法让我每年自动添加它?或者也许是一年的范围?
如有任何建议,我们将不胜感激。谢谢。
<?php echo '<strong>Y: </strong>'.$form->dropDownList($model, 'year', array(date("Y")=>date("Y"),date("Y")-1=>date("Y")-1,date("Y")-2=>date("Y")-2,date("Y")-3=>date("Y")-3 )); ?>
我知道这还不是完全自动化的。但它仍然每年更新自己。
在您的模型中添加此功能
public function getYearsList() {
$currentYear = date('Y');
$yearFrom = 2013;
$yearsRange = range($yearFrom, $currentYear);
return array_combine($yearsRange, $yearsRange);
}
在你的表格中这样称呼它:
$form->dropDownList($model, 'year', $model->getYearsList());
了解 php 的 range 函数