繁体   English   中英

在html的name属性中传递变量

[英]passing a variable in name attribute in html

这段代码从db输出不同的问题,直到while循环用radio类型的MCQ选项终止时为止:

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

但这里的每一个问题的每一个选项都有唯一的问题1的相同名称属性 ,但我想,在接下来的问题name属性会变为问题2等。 请帮忙

如果表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++;
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM