Christophe Costa
Christophe Costa

Reputation: 79

Replicate form select box by array

one last doubt, the foreach is not print in the box

The foreach is outside of the table when is in the middle of the code.

enter image description here

The option values on the table are empty, when is printing outside of the table.

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;

Upvotes: 0

Views: 75

Answers (1)

Samarth
Samarth

Reputation: 773

Hey are you looking for something like this.Please let me know if it helps.

** Suggested Edit in your code

Change this block of your code

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

to

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

What mistake you are making is that you are printing the options string before even the complete table structure has been created properly. Just add these options to table variable and print after every processing is done.

So the overall code will now look like

  $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;

Upvotes: 1

Related Questions