douquanqiao6788 2014-05-12 14:50 采纳率: 100%
浏览 8

是否可以使用PHP动态构建表? [关闭]

Is it possible to dynamically build a table using PHP?

I am using code such as the below to build tables etc. but as there are multiple I was wondering in this case is it possible for the table to be built dynamically based on the SQL query?

For example if I need to build another table then I can copy the below code and just edit the SQL rather than also editing the column headers etc. in the HTML.

Basically some method to simplify the below code so that it is more compact\tidier if being used multiple times on the same page.

Code

  <?php

  error_reporting(E_ALL);
  ini_set('display_errors', 1);

  include('core/connection.php');

  if($conn){

  $stid = oci_parse($conn, "

    SELECT *
    FROM
    (
      SELECT c1, c2, c3, c4
      FROM t1
      ORDER BY c1
    )
    WHERE ROWNUM <= 10

  ");
  oci_execute($stid);

  echo "<table class='table table-hover '>
        <thread>
        <tr>
        <th>c1</th>
        <th>c2</th>
        <th>c3</th>
        <th>c4</th>
        </tr>
        </thread>
        <tbody>";

  while ($row = oci_fetch_array($stid, OCI_NUM)) {

    echo "<tr>";
    echo "<td>" . $row['0'] . "</td>";
    echo "<td>" . $row['1'] . "</td>";
    echo "<td>" . $row['2'] . "</td>";
    echo "<td>" . $row['3'] . "</td>";
    echo "</tr>";
    unset($row);

  }

  echo "</tbody>
        </table>";

  oci_free_statement($stid);
  oci_close($conn);

  }
  • 写回答

7条回答 默认 最新

  • dqmgjp5930 2014-05-12 14:53
    关注

    Yes , it is possible , but you are doing it wrong. HTML that you echo is not valid (some HTML elements have a required closing tag like <table></table> so you should output <table> and </table> in one echo). One of the options for you is to assign your dynamic HTML to a value and then echo the valid HTML.

    $buffer = "<table class='table table-hover '>
               <thread>
                  <tr>
                     <th>c1</th>
                     <th>c2</th>
                     <th>c3</th>
                     <th>c4</th>
                  </tr>
               </thread>
               <tbody>";
    
    while ($row = oci_fetch_array($stid, OCI_NUM)) {
       $buffer .= "<tr>
                     <td>" . $row['0'] . "</td>
                     <td>" . $row['1'] . "</td>
                     <td>" . $row['2'] . "</td>
                     <td>" . $row['3'] . "</td>
                   </tr>";
       unset($row);
    }
    
    $buffer .=  "</tbody>
                 </table>";
    echo $buffer;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助