0

I am looking for a way, to populate an HTML table through PHP when the HTML document is loaded.

I have a php file which creates the required table data:

echo <table id="mytable" ... </table>

The output of this PHP script should be written into the html file during its loading time.

Is there any way to accomplish this? Maybe through JavaScript?

1
  • 1
    PHP is Server side, so it presents the file to you after the server has finished doing what so ever. so if you want to change the content of a html page after the page is already served to the client you need js and more specific ajax if you want to rely on php Commented Nov 3, 2016 at 9:14

2 Answers 2

1

If you have data that is stored in row-major order (that is, your data is an array of rows, rather than columns, of data), you must keep track of the columns to print. This is a typical way to receive data from a MySQL database.

You can do this by looking at your first row, using $columns = array_keys($row), or simply hard-code the column keys if they are known in advance like I have done in the following example.

<?php

$data = [
    [
        "name"  => "Alice",
        "email" => "[email protected]",
        "home_address" => "Alice Lane 61"
    ],
    [
        "name"  => "Bob",
        "age"   => 16,
        "home_address" => "Bob St. 42"
    ]
];

$columns = [ "name", "age", "email", "home_address" ];

?>
<html>
<body>
<table>
    <tr>
        <?php foreach($columns as $column) { ?>
            <th><?php echo $column; ?></th>
        <?php } ?>
    </tr>
    <?php foreach ($data as $row) { ?>
        <tr>
            <?php foreach($columns as $column) { ?>
                <td>
                    <?php 
                        if (isset($row[$column])) {
                            echo $row[$column];
                        } else {
                            echo "N/A";
                        }
                    ?>
                </td>
            <?php } // end $column foreach ?>
        </tr>
    <?php } // end $row foreach ?>
</table>
</body>
</html>

Note that not all rows have the same columns here. This is handled in the if-statement in the table.

This example outputs:

<html>
<body>
    <table>
        <tr>
            <th>name</th>
            <th>age</th>
            <th>email</th>
            <th>home_address</th>
        </tr>
        <tr>
            <td>Alice </td>
            <td>N/A </td>
            <td>[email protected] </td>
            <td>Alice Lane 61 </td>
        </tr>
        <tr>
            <td>Bob </td>
            <td>16 </td>
            <td>N/A </td>
            <td>Bob St. 42 </td>
        </tr>
    </table>
</body>
</html>

Sign up to request clarification or add additional context in comments.

2 Comments

Should the foreachs be closed with <?php endforeach; ?> ?
@TomHoward The foreach is opened by a brace { and closed by another }, see the comment in the code where the closing one is.
0

This will get you started

<table>
  <thead>
    <tr>
      <th>Row One</th>
      <th>Row Two</th>
    </tr>
  </thead>
  <tbody>
    <?php foreach( $array as $key ) { ?>
    <tr>
      <td><?php echo $key[ 'rowOne' ]; ?></td>
      <td><?php echo $key[ 'rowTwo' ]; ?></td>
    </tr>
  <?php } ?>
  </tbody>
</table>

You loop through each row in your array and echo out the information you need. You really need to do some PHP tutorials and you will understand how to loop arrays.

3 Comments

Thanks. But why can´t I simply put the whole creation of the table in a php-file and call it in the html-file through <?php include("myphpfile.php") ?> ?
Separation of code, really you should build the array in your php script and present it to your html page. If you don't need to have php construct html them you shouldn't.
I have to apologize. My problem was the fact, that inline php-scripts are not supported using XAMPP, by default. You have to do some modifications in httpd.conf to use it. This led to my confusion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.