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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:56:29+00:00 2026-06-10T19:56:29+00:00

I have a table will show data from a database. These data count is

  • 0

I have a table will show data from a database. These data count is dynamic. I want to show 4 in each row. However, the problem with the last row. I want if the last row has 1 TD the function will give this td a colspan of 4, so I can show the data in the middle.

NUMBER OF DATA IS 5

###############################################
#           TABLE HEADER(colspan=4)           #
###############################################
||  TD1   ||  TD2  ||   TD3   ||    TD4     ||
||  TD5   ||  

I NEED THIS 
###############################################
#           TABLE HEADER(colspan=4)           #
###############################################
||  TD1   ||  TD2  ||   TD3   ||    TD4     ||
||                  TD5                     ||  

and if there are 6 TDS to show it like this.

###############################################
#           TABLE HEADER(colspan=4)           #
###############################################
||  TD1   ||  TD2  ||   TD3   ||    TD4     ||
||       TD5       ||          TD6          ||  

MY PHP CODE + HTML

<table>
    <tr>
        <th colspan="4">TABLE HEADER</th>
    </tr>
    <tr>
        <? $counter = 0;
                foreach($data as $d)
                {
                    if($counter % 4 == 0)
                    {
                        echo '</tr><tr>';
                    }
                    echo $d['name'];
                    $counter++
                }
        ?>
    </tr>
</table>

Thank’s in advance.

  • 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-06-10T19:56:31+00:00Added an answer on June 10, 2026 at 7:56 pm

    Here’s something to get your started. It is probably not the most elegant, but it will do.

    <tr>
        <? $counter = 0;
           $lastrow = floor($count($data)/4)*4;
           $numinlastrow = $count($data)%4;
                foreach($data as $d)
                {
                    if($counter % 4 == 0)
                    {
                        echo '</tr><tr>';
                    }
                    $colspan = 1;
                    if ($counter >= $lastrow) {
                      if ($numinlastrow == 1) $colspan = 4;
                      if ($numinlastrow == 2) $colspan = 2;
                      if ($numinlastrow == 3) ; // ??
                    }
                    echo "<td colspan='".$colspan."'>";
                    //... contents of table cell here
                    echo "</td>";
                    $counter++
                }
        ?>
    </tr>
    

    If there are 3 table cells, it isn’t really possible with the method above, because colspan can only span whole table columns (no fractions).

    So, instead, put a table inside a table cell spanning the whole row…

    <tr>
        <? $counter = 0;
           $lastrow = floor($count($data)/4)*4;
           $numinlastrow = $count($data)%4;
                foreach($data as $d)
                {
                    if($counter % 4 == 0)
                    {
                      if ($counter >= $lastrow) {
                        echo '</tr><tr><td colspan="4"><table><tr>';
                      }
                      else echo '</tr><tr>';
                    }
    
                    echo "<td>";
                    //... contents of table cell here
                    echo "</td>";
                    $counter++
                }
                if ($numinlastrow != 0) echo '</tr></table></td>'
        ?>
    </tr>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data from database coming out in a loop. Each of these
I have created an archive table which will store data for selecting only. Daily
I have a table called pollData. It will always contain only 1 row. It
if i have a table which has columns with fixed lenght, Will mySQL count
I have 200 users each user will eventually have a reviewINFO table with certain
I have a pivot table, well of course every row will be included in
Fortunately, I know how to fetch data from the database, That's not a problem.
I am trying to show information from database without reloading the page. I have
I have a table in a SQL Server database which stores historical data on
I want to read all rows in a table using system.data.sqlite. As I have

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.