【问题标题】:passing a variable in name attribute in html在html中传递名称属性中的变量
【发布时间】:2018-02-04 17:54:30
【问题描述】:

此代码从 db 输出不同的问题,直到 while 循环使用无线电类型的 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";
    }

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

【问题讨论】:

    标签: php html variables name-attribute


    【解决方案1】:

    如果您的表中有标识符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++;
     }
    

    【讨论】:

      猜你喜欢
      • 2022-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多