How to divide categories in rows of three columns

How to divide categories in rows of three columns

October 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to divide categories in rows of three columns

Dividing Categories into Rows of Three Columns

In web development, especially when working with category lists or product grids, it’s common to want to display items in rows of three columns for better visual organization and responsiveness on different devices. In this post, we’ll guide you through dividing your categories into rows of three using a foreach loop.

The Initial Setup

You have an existing code snippet that retrieves the current child categories:

<?php $_categories = $this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
<div class="item">
    <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a>
</div>
<?php endforeach; ?>

This code will display all the categories in a linear fashion, but we want to organize them into rows of three.

The First Attempt: Opening the Row

To start dividing the categories, you’ll need to open the row div. You can use an if statement and the modulus operator %. Here’s how:

<?php $_categories = $this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
    <?php if ($i % 3 == 0) { ?>
        <div class="row">
    <?php } ?>
    <div class="item">
        <h2><a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?> &raquo;</a></h2>
    </div>
    <?php $i++; ?>
<?php endforeach; ?>

In this code, we initialize a counter $i and check if it’s divisible by 3. If so, we open the row div.

The Second Attempt: Closing the Row

The next step is to close the row div when needed. You’ll need to adjust your logic slightly:

<?php $_categories = $this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
    <?php if ($i % 3 == 0) { ?>
        <div class="row">
    <?php } ?>
    <div class="item">
        <h2><a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?> &raquo;</a></h2>
    </div>
    <?php
        if (($i + 1) % 3 == 0 || ($i + 1 > count($_categories))) {
            echo '</div>';
        }
        $i++;
    ?>
<?php endforeach; ?>

Here, we close the row div when either of two conditions is met:

  • The next item would be the third in a row.
  • There are no more items left to display.

The Third Attempt: Removing the Empty Row

After several attempts, you’ve successfully organized your categories into rows, but there’s an empty row at the beginning. This is because we opened a new row before adding any items:

<div class="category-list">
    <div class="row"></div>
    <!-- Rows with content -->
</div>

To remove this empty row, you can modify your code to open the row div only after adding the first item. This ensures that there’s always some content inside it:

<div class="category-list">
<?php $_categories = $this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
    <?php if ($i % 3 == 0) { ?>
        <div class="row">
            <!-- Adding the first item to open the row -->
            <div class="item">
                <h2><a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?> &raquo;</a></h2>
            </div>
    <?php } else { ?>
        <div class="item">
            <h2><a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?> &raquo;</a></h2>
        </div>
    <?php } ?>
    <?php
        if (($i + 1) % 3 == 0 || ($i + 1 > count($_categories))) {
            echo '</div>';
        }
        $i++;
    ?>
<?php endforeach; ?>
</div>

The Final Solution: Hiding the Empty Row via CSS

While it’s possible to remove the empty row by adjusting your PHP logic, a more efficient approach is to hide it using CSS. This allows you to keep your PHP code clean.

You can use the :empty pseudo-class in CSS:

.row:empty {
    display: none;
}

This will ensure that any row div without content (i.e., an empty row) won’t be displayed on the webpage.

Conclusion

In this post, we’ve walked through the process of dividing categories into rows of three columns using a foreach loop in PHP. We started with the initial setup, then incrementally improved our code to open and close the row divs correctly. Finally, we used CSS to hide any empty rows that were created.

Remember, the key is understanding how to use the modulus operator % and checking for specific conditions to control when to open and close the row div.

For more advanced techniques in PHP development and responsive design, be sure to check out our other resources at Laravel Company. We provide comprehensive guides on Laravel frameworks, PHP best practices, and web development trends.

Catalog PHP

Our network