简体   繁体   中英

How to display div only once in the loop and next div display inside div which run only once

I try to found the solution on google and SO but I haven't got solution.

I have a code something like.

$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++;}?>

What I am doing with the above code is, if $index is less then 4 then the inner text will display else $check will run only once in the loop but it's not working. Also, Notice here I confused where should I closed the displayOnceInwhile closing </div> .

Expected result

<!--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>

Hope This is what you are trying to do is.

<?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>';
?>

You could use following to build your 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM