如何使用循环打印表中的单选按钮

How to print radio buttons in table using loop

本文关键字:单选按钮 打印 何使用 循环      更新时间:2023-09-26

我正在尝试打印表中的单选按钮。我使用php,并使用循环打印单选按钮。

但是当我用这个页面运行浏览器时,它没有显示单选按钮。

代码:

echo "<form>";
echo "<table border='1'><tr><th>Firstname</th><th>da</th></tr>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) 
{
  echo "<tr>";
  echo "<td>" . $row['address'] . "</td>";
  echo "<td><input type="radio" name="q1" value="5" /></td>";
  echo "</tr>";
}
echo "</table>";
echo "</form>";

单选按钮不打印的原因是Echo的性质和"字符.

查看虚线

echo "<td><input type="radio" name="q1" value="5" /></td>";

如果你看到你是如何使用echo的"

Echo想要打印一个字符串,所以它会查找介于"或"之间的内容。

您当前的代码意味着它将尝试打印

"<td><input type=" 

然后它变得有点困惑,因为代码中写着radio这个词,这会让php抓狂并崩溃。

不用担心,你可以在'和"之间切换来做html属性,就像一样

echo '<td><input type="radio" name="q1" value="5" /></td>';
echo "<td><input type="radio" name="q1" value="5" /></td>";

应该是

echo "<td><input type='radio' name='q1' value='5' /></td>";

或者像这样用'"逃离"

echo "<td><input type='"radio'" name='"q1'" value='"5'" /></td>";

使用

echo '<td><input type="radio" name="q1" value="5" /></td>';

而不是

echo "<td><input type="radio" name="q1" value="5" /></td>";
<?php 
echo '<form><table border='1'><tr><th>Firstname</th><th>da</th></tr>';
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>
<td>'.$row['address'].'</td>
<td><input type="radio" name="q1" value="5" /></td>
</tr>';
}
echo '</table>
</form>';
?>