在 html 中传递名称属性中的变量

passing a variable in name attribute in html

此代码从 db 输出不同的问题,直到 while 循环到期,MCQ 选项类型为 radio:

    $sql = "SELECT * FROM questions WHERE `type` IN 
    ('".implode("','",$fin_element)."')";

    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
   // output data of each row

   while($row = $result->fetch_assoc()) {
     echo "<br>";
     echo "Q:" . $row["question_name"]. "<br>";

    echo "<input type='radio' name='question1' value='answer1.1'/    >
          <code>".$row["answer1"]."</code>". "<br>";

    echo "<input type='radio' name='question1' value='answer1.2'/>
          <code>".$row["answer2"]."</code>". "<br>";

    echo "<input type='radio' name='question1' value='answer1.3'/>
          <code>".$row["answer3"]."</code>". "<br>";

    echo "<input type='radio' name='question1' value='answer1.4'/>
          <code>".$row["answer4"]."</code>". "<br>";  
    }
    } else {
   echo "0 results";
    }

但是这里每个问题的每个选项都只有 question1 的相同 name 属性,但我希望下一个问题名称是属性应更改为 question2 等等。请帮助

如果您的 table question 中有标识符,您可以使用它。

但是您可以只使用变量来增加问题编号:

$index = 1 ;
while($row = $result->fetch_assoc()) {
     echo "<br>";
     echo "Q:" . $row["question_name"]. "<br>";

     echo "<input type='radio' name='question".$index."' value='answer1.1'/    >
          <code>".$row["answer1"]."</code>". "<br>";
     //...
     $index++;
 }