Each div 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 Reply

Your email address will not be published. Required fields are marked *