[英]PHP HTML dynamic column headings
我正在使用以下代碼制作一個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>";
您正在尋找PHP MySQL或MySQLI或PDO API已提供的列名屬性。
這是一個示例-http: //www.php.net/manual/zh/function.mysql-field-name.php
祝好運。
使用此代碼可能會對您有所幫助,請使用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>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.