我有三个元素(单选按钮),每个元素都有自己的ID和类。
简单地说它们只是 1、2 、3,但一页上有更多的单选按钮组,我循环它们,所以我需要下一组有 4 5 6,然后是 7 8 9。
我尝试使用以下代码来做到这一点:"
$een = 1;
$twee = 2;
$drie = 3;
$getcats = 'SELECT * FROM questioncat WHERE tid = "'.$conn->real_escape_string($gettemplate['id']).'" ORDER BY id';
$getcatscon = $conn->query($getcats);
while($getcats = $getcatscon->fetch_assoc()){
$werkplekinspectie .= '
<label class="categorytitle">'.$getcats['title'].'</label>
<div class="row">';
$getquestions = 'SELECT * from questions WHERE catid = "'.$getcats['id'].'"';
$getquestionscon = $conn->query($getquestions);
while($getquestions = $getquestionscon->fetch_assoc()){
$werkplekinspectie .= '
<div class="col-md-8">
<p class="questionclass">'.$getquestions['question'].'</p>
</div>
<div class="col-md-4">
<div class="container text-right">
<input type="radio" name="group'.$een.'" id="radio-'.$een.'" value="ok">
<label class="radiotoggle" for="radio-'.$een.'"><span class="radio">Ok</span></label>
<input type="radio" name="group'.$een.'" id="radio-'.$twee.'" value="fout">
<label class="radiotoggle" for="radio-'.$twee.'"><span class="radio">Fout</span></label>
<input type="radio" name="group'.$een.'" id="radio-'.$drie.'" value="nvt">
<label class="radiotoggle" for="radio-'.$drie.'"><span class="radio">N.v.t</span></label>
</div>
</div>';
$een++;
$twee++;
$drie++;
}
$werkplekinspectie .= '
</div>';
}
但再往下单选按钮会触发第一组单选按钮。我做错了什么?
你最好有 1 个计数器,每次都添加相关的偏移量......
$offsetID = 1;
然后
<div class="container text-right">
<input type="radio" name="group'.$een.'" id="radio-'.$offsetID.'" value="ok">
<label class="radiotoggle" for="radio-'.$een.'"><span class="radio">Ok</span></label>
<input type="radio" name="group'.$een.'" id="radio-'.($offsetID+1).'" value="fout">
<label class="radiotoggle" for="radio-'.$twee.'"><span class="radio">Fout</span></label>
<input type="radio" name="group'.$een.'" id="radio-'.($offsetID+2).'" value="nvt">
<label class="radiotoggle" for="radio-'.$drie.'"><span class="radio">N.v.t</span></label>
</div>
然后每次加 3
...$offsetID+=3;
使用您当前的设置 - 您应该为每个计数器添加 3,而不仅仅是增加它们......
$een+=3;
$twee+=3;
$drie+=3;
我的 PHP 很生疏,但你可以用一个计数器变量来完成它,而不必担心按部分递增......
$counter = 0;
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
print '<h2>'. $value . '</h2>'. $counter++ . ' ' . $counter++ . ' ' . $counter++;
}