【问题标题】:select one radio button for each row为每一行选择一个单选按钮
【发布时间】:2014-12-18 09:53:11
【问题描述】:

我正在做一个员工评估系统,我现在在评估表管理。

为了显示评分标准,我使用了 do while 循环 [单选按钮包含在循环中]。 这是我的代码:

<fieldset>
<table border="1">
<tr><br />
<th width="700px">AREAS TO RATE</th>
<th width="100px">1</th>
<th width="100px">2</th>
<th width="100px">3</th>
<th width="100px">4</th>
</tr>

<?php
$rates="select * from tbl_torateareas;";
$raters=mysqli_query($con,$rates);
$raterows=mysqli_fetch_array($raters);?>

<?php do{?>

<tr>
<td><?php echo $raterows['torateareas'];?></td>
<th><input type="radio" name="rad" /></th>
<th><input type="radio" name="rad" /></th>
<th><input type="radio" name="rad" /></th>
<th><input type="radio" name="rad" /></th>
</tr>

<?php }while($raterows=mysqli_fetch_array($raters)); ?>

</table>
</fieldset>

为了选择一个单选按钮,我将所有单选按钮命名为相同的名称 [名称为: rad]。当我这样做时,整个表格只选择了一个单选按钮。例如,对于我的表中的 10 行 - 仅选择了 1 个单选按钮,这应该是 每行一个单选按钮。嗯嗯嗯。

请帮忙。谢谢!

【问题讨论】:

  • 更改每一行的电台名称 :)
  • 先生,我将如何改变它?我在这里使用了循环。

标签: php html mysql radio-button


【解决方案1】:

@MarcoMura 是对的。例如。创建附加变量 $i=0,在每个循环中递增并将其添加到您的名称中,例如第一行中的 rad0,第二行中的 rad1,等等。

虽然@Pupil expanded my answer 并要求将其标记为解决方案,但我知道更有用的一个。这个想法不是使用增量数字,而是使用id(或者可能是element_id,或者在您的数据库中如何调用此列),这也可能使用select * from tbl_torateareas;从您的数据库中获取

所以具体的输入会是这样的

<input type="radio" name="rad[<?php echo $raterows['id'];?>]" />

它为您提供了一个优势,即您将准确地知道数据库中的哪条记录被评级,而不仅仅是您的 html 表中的行序列号。这将有助于避免 DB 中的表在提交表单之前发生更改时出现错误。

其他建议:

1。如果您有 PHP 5.4+ 或 short_open_tag = yes Click to look the echo() docs,您可以使用 &lt;?= 而不是 &lt;?php echo 来使您的代码更具可读性。

2。在这里混合tdth 是不合理的,除非你创建第一个单元格th,因为它是一个标题,而所有其他单元格td,尽管它仍然有效

3。你的循环是多余的,你不需要先获取结果,然后等待它最终是否在while()中获取,只需使用常见的while()语法

while($raterows=mysqli_fetch_array($raters)) {
    // do all the stuff
}

而不是

$raterows=mysqli_fetch_array($raters);
do {
    // stuff
}
while($raterows=mysqli_fetch_array($raters));

我知道您的问题已经得到解答,但也许这将有助于将来编写更好的代码。

【讨论】:

  • 我使用do while循环而不是while循环的原因,先生,我注意到当我使用while循环时,我的table_db的第一行没有显示。 :D 哈哈哈.. 嗯
  • 天哪,先生!您的答案正是我正在寻找的东西:) 部分就是解决方案!非常感谢先生!!!!!!
【解决方案2】:

在循环中添加一个计数器。

您为所有单选按钮分配了相同的name,这就是它们相同的原因。

<?php do{
$i=0;
?>

<tr>
<td><?php echo $raterows['torateareas'];?></td>
<th><input type="radio" name="rad[<?php echo $i;?>]" /></th>
<th><input type="radio" name="rad[<?php echo $i;?>]" /></th>
<th><input type="radio" name="rad[<?php echo $i;?>]" /></th>
<th><input type="radio" name="rad[<?php echo $i;?>]" /></th>
</tr>

<?php
++$i
}while($raterows=mysqli_fetch_array($raters)); ?>

<tr>
<td><?php echo $raterows['torateareas'];?></td>
<th><input type="radio" name="rad[<?php echo $i;?>]" /></th>

并在您的提交单选代码中,循环发布的值。

喜欢:

<?php
if (! empty($_POST['rad'])) {
  $j=0;
  foreach ($_POST['rad'] as $rad) {
    // Access your radio buttons code here.
    ++$j;
  }
}
?>

【讨论】:

  • 啊啊啊啊!谢谢先生!上帝保佑。
  • 先生,您的回答我明白了。我复制了这部分代码
  • 但它只为整个表格选择一个单选按钮。你能检查一下代码吗....
  • @Czarinaaaaa29,是的,它只给你一个张贴变量。但是,你有不同的 $i,请继续努力,你会得到想要的结果。
  • 你的复选框组应该是什么?
【解决方案3】:

这就是解决方案。感谢弗拉德克拉斯先生的进一步阐述!

<input type="radio" name="rad[<?php echo $raterows['id'];?>]" />

【讨论】:

  • 您不需要重新发布您的解决方案,只需标记正确答案即可。
猜你喜欢
  • 2016-09-24
  • 1970-01-01
  • 2020-10-15
  • 2016-01-09
  • 1970-01-01
  • 2012-07-30
  • 2012-04-23
  • 2017-02-28
  • 2015-02-27
相关资源
最近更新 更多