【发布时间】:2013-09-04 02:29:33
【问题描述】:
<select name="year"><?=options(date('Y'),1900)?></select>
<select name="month"><?php echo date('m');?><?=options(1,12,'callback_month')?></select>
<select name="day"><?php echo date('d');?><?=options(1,31)?></select>
PHP
function options($from,$to,$callback=false)
{
$reverse=false;
if($from>$to)
{
$tmp=$from;
$from=$to;
$to=$tmp;
$reverse=true;
}
$return_string=array();
for($i=$from;$i<=$to;$i++)
{
$return_string[]='<option value="'.$i.'">'.($callback?$callback($i):$i).'</option>';
}
if($reverse)
{
$return_string=array_reverse($return_string);
}
return join('',$return_string);
}
function callback_month($month)
{
return date('m',mktime(0,0,0,$month,1));
}
我应该怎么做才能将相应下拉列表中的当前月份和日期作为起始值,就像年份(2013 年)一样。
【问题讨论】:
-
您可以将另一个参数传递给 options() 函数,即当前年份/当前月份。在连接下拉选项的函数内,检查循环中的当前值是否与 paased 参数相同,在选项中添加 selected='selected'
-
你不应该调用数组'$return_string'。为了可读性,将其称为“$options_array”或其他名称。