fr3d
fr3d

Reputation: 685

While loop need to create new div every 5 elements

I've got a while loop that displays 50 logos. But what I need is another loop that creates a new div(.autogrid_wrapper .cte .block) every 5 images.

<?php
                        $cn = 1;
                        while($result->next()) {
                            if ($cn % 5 == 0) {
                            ?>
                            <div class="autogrid_wrapper cte block">
                                <div class="inner">
                                <?php } ?>
                                <div class="ce_card autogrid-type_cte n5 one_fifth autogrid_mode_auto autogrid <?php echo $class2; ?> <?php echo $class; ?> block">
                                    <div class="card_wrapper">
                                        <a class="download_image" title="<?php echo $result->name; ?>"
                                        <div class="ce_image attribute image">
                                            <div class="ce_image block">
                                                <figure class="image_container">
                                                    <img src="<?php echo $imageVar->path; ?>" onerror="this.onerror=null; this.src='files/Intershop/media/images/customers/<?php echo $rest; ?>.png'" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
                                                </figure>
                                            </div>
                                        </div>
                                        </a>
                                    </div>
                                </div>
                                <div class="clear autogrid_clear"></div>
                                <?php if ($cn % 5 == 0) { ?>
                                </div>
                            </div>
                                <?php
                                }
                                $cn++;
                                ?>
                        <?php } ?>

I hope you guys can help me.

Upvotes: 0

Views: 659

Answers (2)

Kevin Yan
Kevin Yan

Reputation: 1236

Ok , after ask you in comments I know what's your purpose. You wanna print div(.autogrid_wrapper .cte .block) before 1st item and close this div after while walk through 5th item and so on.

$cn = 1;
while($result->next()) {
    if($cn % 5 == 1) {
        //div(.autogrid_wrapper .cte .block)
    }

    // HTML wraps image

    if($cn % 5 == 0) {
        //print the close tag of div(.autogrid_wrapper .cte .block)
    }
    $cn ++;
}

Embed this flow control in your code, I think this will work.

Upvotes: 2

Maninderpreet Singh
Maninderpreet Singh

Reputation: 2587

Try with this hope it's helps

<?php 
$cn = 1;
while($result->next()) { 
     if ($cn % 5 == 0) { //check if number is divided by 5 like 5,10,15 etc
           //new div'
     }

    YOUR HTML

      if ($cn % 5 == 0) {
           //close div'
     } 
     $cn++;
     } ?>

Upvotes: 0

Related Questions