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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:42:05+00:00 2026-06-13T02:42:05+00:00

As title states, my objective is to produce an HTML table using data from

  • 0

As title states, my objective is to produce an HTML table using data from a MySQL database.

The general method I am using is sound, as I have written manually a series of cells, but I am trying to scale it to include large numbers of fields, and as such am trying to write the HTML for the cells using PHP and foreach loops with arrays. I’ve written out the context and steps of my working to both give context to my problem and provide general help to people in a similar situation. I know, however, that the problem I have lies specifically in one section of code – indicated clearly below if you want to skip right to it.

Step 1 define arrays for the titles I’d like the fields to have and the column names in the MySQL db:

$titles  = array('Name', 'Age', 'Height', 'Weight');
$headers = array('q1', 'q2', 'q3', 'q4');

Step 2: connect to the database ($username and $password previously defined):

try {
    $pdo = new PDO('mysql:host=localhost; dbname=db01', $username, $password);
    $pdo->exec("SET CHARACTER SET utf8");

Step 3: Create my SQL statement via a few concatenations, and fetch the $result:

$sql ="SELECT";

foreach(array_combine($headers, $titles) as $header => $title) {
    $sql .= "`$header` AS `$title`,";
}

$sql .= "`q5` AS `Eye Color`";
$sql .= "FROM samdata.CO_data";


$result = $pdo->query($sql);

Step 4: Create $html_table by concatenating static HTML and the results from the above SQL query

if($result !== false) {
    $html_table = '<table><tr>';
    foreach($titles as $title) {
        $html_table .= "<th> $title </th>";                          
    }

    $html_table .='</tr> <tr>';


//*********PROBLEM SECTION BELOW **************************
    foreach(array_combine($result, $titles) as $row => $title) {                            
        $html_table .= "<td>' .$row\['$title'\]. '</td>";
    }
}
//*********PROBLEM SECTION ABOVE **************************

$html_table .= '</tr> </table>';
$conn = null;   

echo $html_table;       
}

I know the problem lies with the section indicated, because I’ve test all the other parts, and if instead of combining arrays / foreach loops I manually write out, for example:

foreach($result as $row) {
             $html_table .= '
             <tr>
                <td>' .$row['Names']. '</td>
                <td>' .$row['Age']. '</td>
                <td>' .$row['Height']. '</td>
            </tr>';
}

In place of the loop, then it works fine and displays the data for each result for those headings, the problem being I have hundreds and want to be more elegant (and lazy) than writing them all out one by one!

Where am I going wrong? Many thanks in advance for your help.

  • 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-13T02:42:06+00:00Added an answer on June 13, 2026 at 2:42 am

    Your are not looping correctly. $result is a PDOStatement which you must fetch. Try

    foreach( $result->fetchAll() as $row ) {
        $html_table .= '<tr>' . "\n";
        foreach( $row as $col ) {
            $html_table .= '<td>' .$col. '</td>';
        }
        $html_table .= '</tr>' . "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title states, If I have a jQuery listener tied to an HTML
As the title states I have added mysql dependency: val appName = report val
As the title states, I am using devise. I came upon this link that
As the title states, I have a C# console app which uses interop to
As the title states. I have an MVC 3 Razor view which i would
Like the title states, does anyone out there have a clear way to implement
As the title states. Our current method of deployment installs MVC1 onto the user's
as the title states, I am trying to add two floats using javascript/jquery, and
As the title states, I have a function that causes a div to fadeOut
As the title states, I made an Ajax call to a .html file which

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.