复制表单按数组选择框

时间:2017-03-16 10:43:18

标签: php html arrays forms

最后一个疑问,foreach不打印在盒子里

当代码位于代码中间时,foreach位于表外。

enter image description here

当在表格外打印时,表格上的选项值为空。

enter image description here

$options = array( "0.00","0.05","0.10","0.15","0.20","0.25","0.30","0.35","0.40","0.45","0.50","0.55","0.60","0.65","0.70","0.75","0.80","0.85","0.90","0.95","1" ); // ◄■■ OPTIONS ARE STATIC (ALWAYS THE SAME).

        $table = $table."</tr>";

        for($i = 0 ; $i <$uCount ; $i++ ){
        $table = $table."<tr><td>".$utilizador[$i]."</td>";

        for($j=0;$j<$carCount;$j++){
        $val= htmlspecialchars($s2[$j], ENT_HTML5 | ENT_COMPAT, 'UTF-8');

        $table = $table."<td>"."<select name=\'corp_resp&{$row_menuid['menuId']}&{$_SESSION['UtilizadorID']}&{$dateTime}&{$toEchosave}'.$val.'_'.$utilizador[$i].'\>";
        foreach ($options as $opt)
        {
               echo "<option value='$opt'>$opt</option>\n";
               echo "</select>\n"."</td>";
          }
        }
    $table = $table."</tr>";
}
$table = $table."</form>";
echo $table;

1 个答案:

答案 0 :(得分:1)

嘿,你在找这样的东西。如果有帮助,请告诉我。

**建议编辑代码

更改此代码块

foreach ($options as $opt)
{
     echo "<option value='$opt'>$opt</option>\n";
     echo "</select>\n"."</td>";
}

 foreach ($options as $opt)
 {
      $table = $table."<option value='$opt'>$opt</option>\n";
 }
 $table = $table."</select>\n"."</td>";

您犯的错误是在正确创建完整的表结构之前打印选项字符串。 只需将这些选项添加到表变量中,并在每次处理完成后打印。

所以整体代码现在看起来像

  $form = "<form onsubmit=\"return validate();\" id=\"teste\" method=\"post\" action=\"teste.php\">";
  $table = $form."<table><tr><td>Parametro</td>";
  $carCount = count($s2);
  $uCount = count($utilizador);
  for($x = 0; $x < $carCount; $x++){
      $val= htmlspecialchars($s2[$x], ENT_HTML5 | ENT_COMPAT, 'UTF-8');
      $table= $table."<td>".$val."</td>";
  }

  $options = array( "0.00","0.05","0.10","0.15","0.20","0.25","0.30","0.35","0.40","0.45","0.50","0.55","0.60","0.65","0.70","0.75","0.80","0.85","0.90","0.95","1" ); // ◄■■ OPTIONS ARE STATIC (ALWAYS THE SAME).

          $table = $table."</tr>";

          for($i = 0 ; $i <$uCount ; $i++ ){
          $table = $table."<tr><td>".$utilizador[$i]."</td>";

          for($j=0;$j<$carCount;$j++){
          $val= htmlspecialchars($s2[$j], ENT_HTML5 | ENT_COMPAT, 'UTF-8');

          $table = $table."<td>"."<select name=\'corp_resp&{$row_menuid['menuId']}&{$_SESSION['UtilizadorID']}&{$dateTime}&{$toEchosave}'.$val.'_'.$utilizador[$i].'\>";
          foreach ($options as $opt)
          {
                 $table = $table."<option value='$opt'>$opt</option>\n";
            }
             $table = $table."</select>\n"."</td>";
          }
      $table = $table."</tr>";
  }
  $table = $table."</form>";
  echo $table;