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

  • SEARCH
  • Home
  • 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 7072489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:48:54+00:00 2026-05-28T05:48:54+00:00

I have a pretty standard database like: id | name | last_name | gender

  • 0

I have a pretty standard database like:

id | name | last_name                | gender 
-----------------------------------------------
1  | John | Doe                      | Male
2  | Jane | Smith Dolores Clayborne  | Female
3  | Paul | Paulson                  | Male

and I want to display it in a table, but every row in the DB needs to be a column in HTML table:

id        | 1         | 2         | 3      
---------------------------------------------
name      | John      | Jane      | Paul
---------------------------------------------
last_name | Doe       | Smith     | Paulson
          |           | Dolores   |
          |           | Clayborne |
---------------------------------------------
gender    | Male      | Female    | Male 

If I go with:

foreach($data as $row) {
echo "<tr><td>" . $row->id . "</td></tr>";
echo "<tr><td>" . $row->name . "</td></tr>";
echo "<tr><td>" . $row->last_name . "</td></tr>";
echo "<tr><td>" . $row->gender . "</td></tr>";
}

I get all the data in one long column. How do I break the column after every SQL row?

Note: $data is an array of objects that contain properties with the field values (you can probably figure that out from the example).

EDIT:

I found the solution, see my answer, it’s simple and elegant.

  • 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-28T05:48:55+00:00Added an answer on May 28, 2026 at 5:48 am

    I figured it out, turns out it’s a pretty simple solution with few cycles and pre-populating an array before writing rows and cells.

    I tested it, works like a charm. 😉 It might be useful for someone, so here it is:

    foreach($records as $key => $row) {
           foreach($row as $field => $value) { 
               $recNew[$field][] = $value;
           }
    }
    //This creates a new array composed/transposed with the field names as keys and
    //the "rowed" values as sub-arrays.
    
    echo "<table>\n";
    
    foreach ($recNew as $key => $values) // For every field name (id, name, last_name, gender)
    {
        echo "<tr>\n"; // start the row
        echo "\t<td>" . $key . "</td>\n" ; // create a table cell with the field name
            foreach ($values as $cell) // for every sub-array iterate through all values
            {
               echo "\t<td>" . $cell . "</td>\n"; // write cells next to each other
            }
        echo "</tr>\n"; // end row
    
    }
    
    echo "</table>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty standard database sitting on SQL Server. To manage the data
Basically I do have pretty simple database that I'd like to index with Lucene.
I have a pretty standard attendance database design, but I want to be alerted
I have a pretty standard table set-up in a current application using the .NET
I have a pretty standard django app, and am wondering how to set the
I have a pretty standard Repository pattern going, where repositories are injected into my
I am currently writing an financial application, and we have a pretty standard customer
I have what I imagine to be a pretty standard web-interface. There are 4
I have a Rails application that right now is pretty standard: Heroku/PostgreSQL backend, users
I have a pretty standard setup, with a many-to-many relationship between Departments and Employees.

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.