0

I want a pdo code for fetching data from database into a table dynamically when the page is opened.It took me an entire day but I still didnt get an appropriate code,PLEASE HELP ME OUT

<div class="card">
            <div class="card-header">
                <h2>Selection Example <small>Ensure that the data attribute [data-identifier="true"] is set on one column header.</small></h2>
            </div>

            <table id="data-table-command" class="table table-striped table-vmiddle">
                <thead>
                    <tr>
                        <th data-column-id="id" data-type="numeric">ID</th>
                        <th data-column-id="sender">Sender</th>
                        <th data-column-id="received" data-order="desc">Received</th>
                        <th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
                    </tr>
                </thead>
                <tbody>
                                            <tr>
                        <td>10238</td>
                        <td>eduardo@pingpong.com</td>
                        <td>14.10.2013</td>
                    </tr>
                    <tr>
                        <td>10243</td>
                        <td>eduardo@pingpong.com</td>
                        <td>19.10.2013</td>
                    </tr>
                    <tr>
                        <td>10248</td>
                        <td>eduardo@pingpong.com</td>
                        <td>24.10.2013</td>
                    </tr>
                    <tr></tbody>
            </table>
        </div></div>
Pihu
  • 7
  • 3

1 Answers1

0
$data = $pdo->query("SELECT * FROM t")->fetchAll();
if ($data) {
    echo "<table><tr>";
    foreach (array_keys($data[0] as $col)) {
        echo "<th>$col</th>";
    }
    echo "</tr>";
    foreach($pdo->query("SELECT * FROM t") as $row) {
        echo "<tr>";
        foreach ($row as $col) {
            echo "<td>$col</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Thanks for the solution but how do i feed my data in the table above by using a function call when the page is opened – Pihu Mar 27 '17 at 12:00