PHP HTML动态列标题

时间:2014-06-28 16:37:37

标签: php html sql

我使用以下代码制作HTML表格。我在网上搜索 PHP获取列标题,但无法找到任何信息(仅有关于行的信息)。是否可以使用while循环并从SQL打印列标题,而不是像下面那样硬编码?

代码

  $stid = oci_parse($conn, "

    SELECT *
    FROM
    (
     SELECT   orders.order_no, orders.porder_no, orders.date, order_totals.value
     FROM     orders, order_totals
     WHERE    orders.order_no = order_totals.order_no
     AND      orders.account_no = '" . $_SESSION['session_account'] . "'
     ORDER BY orders.order_no DESC
    )
    WHERE ROWNUM <= 15

  ");
  oci_execute($stid);

  echo "<table class='table'>
        <thread>
        <tr>
        <th>Order No</th>
        <th>Purchase Order No</th>
        <th>Date</th>
        <th>Value</th>
        </tr>
        </thread>
        <tbody>";

  while ($row = oci_fetch_array($stid, OCI_NUM)) {
      echo "<tr>";
      echo '<td><a href="view.php?id=' . $row[0] . '">' . $row[0] . '</a></td>';
      for ( $ii = 1; $ii < count($row); $ii++ ) {
          echo "<td>" . $row[$ii] . "</td>";
      }
      echo "</tr>";
  }

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

2 个答案:

答案 0 :(得分:0)

您正在寻找已由PHP MySQL或MySQLI或PDO API提供的列名属性。

以下是一个示例 - http://www.php.net/manual/en/function.mysql-field-name.php

祝你好运。

答案 1 :(得分:0)

使用此代码可能会对您有所帮助,请使用oci_field_name()获取列名称

  echo "<table class='table'>
            <thread>
            <tr>";
    $ncols = oci_num_fields($stid);// to get column number

    for ($i = 1; $i <= $ncols; $i++) {// index start from 1
        $column_name  = oci_field_name($stid, $i);

    echo "<th>".$column_name."</th>";

    }
    echo "</tr>
            </thread>
            <tbody>";