Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在下面的动态下拉菜单中插入这些值_Php_Html - Fatal编程技术网

Php 如何在下面的动态下拉菜单中插入这些值

Php 如何在下面的动态下拉菜单中插入这些值,php,html,Php,Html,我有一个动态下拉菜单,显示数字1-10中的每个值: $min_year = 1; $max_year = 10; $years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012 $durationHTML = ''; $durationHTML .= '<select name="duration" id="durationDrop">'.PHP_EOL; $dura

我有一个动态下拉菜单,显示数字1-10中的每个值:

$min_year = 1;
$max_year = 10;
$years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012
$durationHTML = '';
$durationHTML .= '<select name="duration" id="durationDrop">'.PHP_EOL; 
$durationHTML .= '<option value="">Please Select</option>'.PHP_EOL;  
foreach ($years as $year) {
    $durationHTML .= "<option>$year</option>".PHP_EOL;  // if no value attribute, value will be whatever is inside option tag, in this case, $year
}
$durationHTML .= '</select>';
我的问题是,如何动态显示上述值

更新:

$min_year = 1;
$max_year = 10;
$years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012
$durationHTML = '';
$durationHTML .= '<select name="duration" id="durationDrop">'.PHP_EOL; 
$durationHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

for($i = 0; $i < sizeof($years); $i++)
{
  $current_year = $years[$i];
  $durationHTML .= "<option>{$current_year}</option";
  if($i != (sizeof($years) - 1 )) //So you don't try to grab 11 because it doesn't exist.
  {
    $next_year = $years[$i+1];
    $durationHTML .= "<option>{$current_year}/{$next_year}</option>";
  }
}

$durationHTML .= '</select>'; 

可能有几种方法可以做到这一点

最简单的方法是将foreach循环更改为简单的for循环,然后获取下一个值并使用它形成分数。我这样做是为了更容易阅读,但是您不需要将当前年和下一年存储在变量中,您可以直接从数组中回显它们

for($i = 0; $i < sizeof($years); $i++)
{
  $current_year = $years[$i];
  echo "<option>{$current_year}</option";
  if($i != (sizeof($years) - 1 )) //So you don't try to grab 11 because it doesn't exist.
  {
    $next_year = $years[$i+1];
    echo "<option>{$current_year}/{$next_year}</option>";
  }
}
for($i=0;$iecho“{$current_year}这样的东西应该适合你

$min_year = 1;
$max_year = 10;
$years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012
$durationHTML = '';
$durationHTML .= '<select name="duration" id="durationDrop">'.PHP_EOL; 
$durationHTML .= '<option value="">Please Select</option>'.PHP_EOL;  
foreach ($years as $year) {
    $durationHTML .= "<option>$year</option>".PHP_EOL;  
    if ($year != $max_year) {
         $nextYear = $year + 1;
         $durationHTML .= "<option>$year / $nextYear</option>".PHP_EOL;              
    }
}
$durationHTML .= '</select>';
$min_year=1;
$max_year=10;
$years=范围($min_year,$max_year);//返回数值为1900-2012的数组
$durationHTML='';
$durationHTML.=''.PHP\u EOL;
$durationHTML.='请选择'.PHP\u EOL;
foreach($年为$年){
$durationHTML.=“$year”.PHP\u EOL;
如果($year!=$max\u year){
$nextYear=$year+1;
$durationHTML.=“$year/$nextYear”。PHP_EOL;
}
}
$durationHTML.='';
for($i = 0; $i < sizeof($years); $i++)
{
  $current_year = $years[$i];
  echo "<option>{$current_year}</option";
  if($i != (sizeof($years) - 1 )) //So you don't try to grab 11 because it doesn't exist.
  {
    $next_year = $years[$i+1];
    echo "<option>{$current_year}/{$next_year}</option>";
  }
}
$min_year = 1;
$max_year = 10;
$years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012
$durationHTML = '';
$durationHTML .= '<select name="duration" id="durationDrop">'.PHP_EOL; 
$durationHTML .= '<option value="">Please Select</option>'.PHP_EOL;  
foreach ($years as $year) {
    $durationHTML .= "<option>$year</option>".PHP_EOL;  
    if ($year != $max_year) {
         $nextYear = $year + 1;
         $durationHTML .= "<option>$year / $nextYear</option>".PHP_EOL;              
    }
}
$durationHTML .= '</select>';