循环将三个元素增加一个数字

时间:2018-12-28 15:52:57

标签: php html

我有三个元素(单选按钮),每个元素都有自己的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>';
}

但是单选按钮进一步向下触发第一组单选按钮。我在做什么错了?

2 个答案:

答案 0 :(得分:2)

最好使用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;

答案 1 :(得分:1)

我的PHP相当生锈,但是您可以使用一个计数器变量将其关闭,而不必担心按段递增...

$counter = 0;

$arr = array(1, 2, 3, 4);

foreach ($arr as &$value) {
    print '<h2>'. $value . '</h2>'. $counter++ . ' ' . $counter++ . ' ' . $counter++;
}