Question

So I am trying to use PHP to generate some content with the criteria of the nth loop needs to either open or close a div element. So my goal is to display 8 elements on the page and on the 9th element, I would like to close the div for the 8th element and open a new one for the 9th if there are any more image. Here is my code.

$images; // object
$i = 1;

echo '<div class="outer-container">';

while ( $images->have_images() ) {

     if ( $i === 1 ) {
          echo '<div class="inner-container">';
     }

     echo 'content here';

     if ( $i === 8 || $images->total === $images->current + 1 ) {
          echo '</div><!--.inner-container-->';
     }

     $i++;
}

echo '</div><!--.outer-container-->';

End result should look something like this:

<div class="outer-container">

<div class="inner-container">
    content here
    content here
    content here
    content here
    content here
    content here
    content here
    content here
</div><!--.inner-container-->

<div class="inner-container">
    content here
    content here
    content here
</div><!--.inner-container-->

</div><!--.outer-container-->

I searched around and came to a conclusion that I have to probably use modulus but I am not certain how I can incorporate that into my code.

Thanks for looking.

Était-ce utile?

La solution

if ( $i % 8 == 0) {
      echo '</div><!--.container--><div class="container">';
}

Mod operator returns the remainder. You can simply state $i % 8 or $i % 8 == 0 or $i % 8 === 0 as john stated

But you will need to also open the next div in your conditional depending on how you structure this. An example:

<div class="outer-container">
    <div class="inner-container">
        <?php
        $images; // object
        $i = 1;

        while ( $images->have_images() ) {

             echo 'content here';
             //print_r($i % 8);
             if ( $i % 8 == 0 && $images->total > 8) {
                  echo '</div><!--.inner-container--><div class="inner-container">';
             }

             $i++;
        }
        ?>
    </div>
</div>

Added conditional in case your images total happens to be 8 it won't create a rogue empty div.

I'm trying to understand your comment. Are you saying you have an outer container, then an inner container, and inside this inner container you want the loop you defined in the question where each group of 8 is contained in a container div?

I added a print r in the loop you can uncomment to verify $i is doing what you want it to. By this code and the fact that $i begins at 1 you should not experience the closing div conditional prematurely.

Autres conseils

Modulus is indeed what you're looking for:

if ( $i % 8 === 0 ) {
    echo '</div><!--.container-->';
}

You forgot to open a new div

modify the following code:

if ( $i === 8 || $images->total === $images->current + 1 ) {
    echo '</div><!--.container-->';
    echo '<div class="container">'; //ADD THIS LINE!!!
}

and after you get out of the while add the closing </div> tag

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top