Each div max contains only 3 items using foreach loops in Php

<?php
$item_count=1;
$items_block=3;
?>
<div class="wrapper">
    <?php if(!empty($list)){ ?>

        <div class="item_block">
            <?php foreach ($list as $val){ ?>
                <div>Item</div>
                
                <?php 
                if($item_count % $items_block==0){ ?>
                    </div>
                    <div class="item_block">
                <?php 
                } 
                $item_count++;
                ?>
                    
            <?php endforeach; ?>
        </div>
    <?php } ?>
</div>

Leave a Comment