drl2051 2013-09-09 17:07
浏览 21
已采纳

php中的唯一无线电名称while循环

Well, I've following Php code which generate a table with radio button so that user can select only one radio button in EVERY row. But to select only one radio button in Every row it's must be a unique name in EVERY row, Right ? But I can't get any idea how do i set a unique name in while loop. Can you guys give me any idea or solutions ?

Php code:

$action = htmlspecialchars($_SERVER['PHP_SELF'])."?class=$class_from";  
echo "<form method='post' action='$action' name='attendence'/>";
echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='200'><b>Student Name</b></td>";    
echo "<td class='tdhead' valign='top' width='250'><b>Roll No</b>
</td>";             
echo "<td class='tdhead' valign='top' width='250'><b>Class Name</b>
</td>";             
echo "<td class='tdhead' valign='top' width='200'><b>Present / Not present</b>
</td>";                 
echo "<td class='tdhead' valign='top' width='200'>Check All <input type= 'checkbox' 
onclick='checkAll(this)' /></td>";                  
echo "</tr>";


while($res2 = mysql_fetch_array($sql2))
{

$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);

echo "<tr>";                
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";        
echo "<td class='tdhead2' valign='top'>
<input type='radio' name='ch[]' value='1' />&nbsp;<input type='radio' name='ch[]' 
value='1' />
</td>";                     
echo "<td class='tdhead2' valign='top'>&nbsp;</td>";                
echo "</tr>";               

}

echo "<tr>";                
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'>&nbsp;</td>";             
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'><input type='submit' value='Record' name='Submit' 
class='submit' /></td>";                
echo "</tr>";               

echo "</table>";
echo "</form>";

Thanks for your help.

Update:

echo "<td class='tdhead2' valign='top'>";
echo '<input type="radio" name="ch'.$counter++.'[]" value="1">';
echo "&nbsp;";
echo '<input type="radio" name="ch'.$counter.'[]" value="0">';
echo "</td>";                       
  • 写回答

4条回答 默认 最新

  • douche3791 2013-09-09 17:13
    关注

    Instead of making the name just ch[] you can make the name ch[SOME_UNIQUE_VALUE] for each row. Usually something like an id number is used. Perhaps $roll if that is unique to each student. That will also allow you to know which id the radio button pertains to. On form submit, you can foreach($_POST['ch'] as $id=>$value) and get the id out.

    Edit:

    Example code:

    //start the counter variable
    $counter = 1;
    while($res2 = mysql_fetch_array($sql2))
    {
    
        $sname = inputvalid($res2['sname']);
        $roll = inputvalid($res2['roll']);
        $class = inputvalid($res2['class']);
    
        echo "<tr>";
        echo "<td class='tdhead2' valign='top'>$sname</td>";
        echo "<td class='tdhead2' valign='top'>$roll</td>";
        echo "<td class='tdhead2' valign='top'>$class</td>";
        echo "<td class='tdhead2' valign='top'>";
    
        //echo each radio with the counter for this row
        echo "<input type='radio' name='ch_{$counter}[]' value='0' />&nbsp;";
        echo "<input type='radio' name='ch_{$counter}[]' value='1' />";
    
        echo "</td>";
        echo "<td class='tdhead2' valign='top'>&nbsp;</td>";
        echo "</tr>";
    
        //add one to the counter
        $counter++;
    }
    

    Please note, unless you do like I suggested above, you will not be able to know which student each radio group goes to. You can assume that the radio buttons on group row 1 belongs to the student in row 1, but you might not know which student is in row 1. Which is why I suggest you put a unique id that goes with each student with each radio button instead of a counter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么