2017-03-27 67 views
0

我想要一个pdo代码,用于在页面打开时动态地从数据库中将数据提取到表中。它花了我整整一天的时间,但我仍然没有得到适当的代码,请帮助我OUT pdo代码用于在表中动态获取数据

<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>[email protected]</td> 
         <td>14.10.2013</td> 
        </tr> 
        <tr> 
         <td>10243</td> 
         <td>[email protected]</td> 
         <td>19.10.2013</td> 
        </tr> 
        <tr> 
         <td>10248</td> 
         <td>[email protected]</td> 
         <td>24.10.2013</td> 
        </tr> 
        <tr></tbody> 
      </table> 
     </div></div> 

回答

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>"; 
} 
+0

感谢您的解决方案,但如何让我养活我的数据上表中被打开页面时使用函数调用 – Pihu