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 6562171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:40:39+00:00 2026-05-25T13:40:39+00:00

I got an html table, and I use some loop to get some data,

  • 0

I got an html table, and I use some loop to get some data, this data is displaying this way:

<tr><td>Data</td></tr>
... next loop

But I wan’t it to close table row (tr) every 2 or even 3 loops. So the data may look like this:

<tr>
<td>Data</td>
<td>Data1</td>
<td>Data2</td>
</tr>

...next loop...

Will you help me with this?

  • 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-25T13:40:40+00:00Added an answer on May 25, 2026 at 1:40 pm

    If you have some counter in your loop you can use Modulus for this.

    It’s basically what’s left of a number if you divide it.

    Example:

    for($i = 1; $i < 11; $i++) {
        if ($i % 2 === 0) {
            print('this is printed every two times');
        }
        if ($i % 3 === 0) {
            print('this is printed every three times');
        }
    }
    

    If you use a foreach() in stead you should just make a counter yourself (as Link stated you could also use the key of an array if it contains nice incremental keys):

    $i = 1;
    foreach($array as $item) {
        if ($i % 2 === 0) {
            print('this is printed every two times');
        }
        if ($i % 3 === 0) {
            print('this is printed every three times');
        }
        $i++;
    }
    

    Or in your specific case it would look something like:

    print('<tr>');
    $i = 1;
    foreach($array as $item) {
        if ($i % 3 === 0) {
            print("</tr>\n<tr>");
        }
        print("<td>$item</td>\n");
        $i++;
    }
    print('</tr>');
    

    The above is just a basic example.

    You should also check whether the number of the columns is balanced and if not either add a colspan or an empty columns to balance it.

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

Sidebar

Related Questions

I'm inserting some HTML into a MySQL database table. But when I get it
I've got PHP and HTML code stored in a database table. When I get
I've got an html table: <table><tr> <td>M1</td> <td>M2</td> <td>M3</td> <td>M4</td> </tr></table> and a simple
I've got HTML code that roughly looks like this: <li id=someid-11> <img src=... alt=alt
It's a really simple problem. I've got HTML code like this : <div> <img
I got some html text, which contains all kinds of html tags, such as
I've got an MVC3 Read Only view that contains a table displaying properties for
I've got an HTML table that I'm trying to parse the information from. However,
I've got a form that looks like this: <form method=post> {% csrf_token %} <table>
I've got this little snippet that I use for row highlighting on an XSLT

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.