如何在循环中仅显示div一次,而在div内部仅显示一次的下一个div显示

我尝试在google和SO上找到解决方案,但是我没有解决方案。

我有类似的代码。

$index = 0;
while (some condition here) { 
         if ($index < 4) {?>
            <div class="first4">
              <p>Some text here</p>
            </div>
    <?php }
        else{
            $check=0;
            if ($check==0){?>
              <div class="displayOnceInwhile">
            <?php $check=1; }?>
            <div class="Insideaboveclass"></div>

    <?php } 

$index++;}?>

我在上面的代码中所做的是,如果$index小于4,则内部文本将显示否则$check将仅在循环中运行一次,但无法正常工作。另外,请注意,在这里我感到困惑,我应该在哪里关闭displayOnceInwhile和关闭</div>

预期结果

<!--first 4 will display-->
    <div class="first4"><p>Some text here</p></div>
     <div class="first4"><p>Some text here</p></div>
     <div class="first4"><p>Some text here</p></div>
     <div class="first4"><p>Some text here</p></div>
<!--Set will display like this-->
     <div class="displayOnceInwhile">
      <div class="Insideaboveclass"></div>
    </div>
zhoujiang1984 回答:如何在循环中仅显示div一次,而在div内部仅显示一次的下一个div显示

您可以使用以下内容来构建HTML

<?php
$first = '<div class="first4" ><p > Some text here </p ></div >';
$firstOpen = '<div class="first4" ><p > Some text here </p >';
$firstClose = '</div>';
$once = '<div class="displayOnceInwhile"><div class="InsideaboveClass"></div>';

$index = 0;
while ($index < 3) {
    echo $first;
    $index++;
}
echo $firstOpen;
echo $once;
echo $firstClose;
,

希望这就是您想要做的。

<?php
$check = 0;
$index = 0;
while (some condition here) { 
    if ($index < 4) {
        echo '<div class="first4"><p>Some text here</p></div>';
    } else {
        if ($check==0){
            echo '<div class="displayOnceInwhile">';
            $check=1;
        }
        echo '<div class="InsideaboveClass"></div>';
    }
    $index++;
}
echo '</div>';
?>
本文链接:https://www.f2er.com/3133570.html

大家都在问