Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6751659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:57:11+00:00 2026-05-26T12:57:11+00:00

I have modified the tpl used for the grid view to show 3 columns

  • 0

I have modified the tpl used for the grid view to show 3 columns of content, my only issue that the code below creates unneeded divs for the view. I have a maximum of 9 items that should be output in 3 rows, 3 per column. What’s the best way to modify the code below? to prevent the extra divs from being output.

<?php foreach ($rows as $row_number => $columns): ?>
  <div>
    <?php foreach ($columns as $column_number => $item): ?>

        <?php print $item; ?>

    <?php endforeach; ?>
  </div>
<?php endforeach; ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:57:12+00:00Added an answer on May 26, 2026 at 12:57 pm

    I would drop that notation of foreach syntax (but I think that’s pretty personal 🙂 )

    You could use the modulo to check if you just had 3 columns (if I understood your question).

    <?php
    
    // I´m assuming that $column_number is a zero based index
    // if thats not the case you should add a counter to keep track of column numbers or if it is in sequence but isn't zero based you could easily update the calculates based in your starting index
    
    foreach ($rows as $row_number => $columns) {
        foreach ($columns as $column_number => $item) {
            if ($column_number == 0 || $column_number%3 == 0) {
                print '<div>';
            }
            print $item;
            if ($column_number == 2 || $column_number%3 == 2) {
                print '</div>';
            }
        }
        // prevent open div tags
        $total_columns = count($columns);
        if ($total_columns > 0 && ($total_columns < 3 || $total_columns%3 != 0)) {
            print('<div>');
        }
    }
    

    I’ve also dropped all those opening and closing tags of php for readability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Fullcalendar with the week view. I have modified fullcalendar to show
I have modified my code, I am making a decoder that decodes as follows:
I have modified the Android Bluetooth Chat code so that bytes received from the
I have modified a working Windows service that had always been starting beforehand. After
I have a dataset that I have modified into an xml document and then
Recently I have modified my code to While taking input form STDIN, I moved
I have modified my etc/hosts file (under Windows 7), so that www.example.com and demo.example.com
I have modified my application, so that when a new user is registered, it's
I have modified a UISegmentedControl so that it is vertical instead of horizontal- it
I have modified bx.slider to have the thumbnails underneath text content with an image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.