简体   繁体   English

使用SQL的PHP​​动态HTML表

[英]PHP dynamic HTML table using SQL

Background 背景

I am using the below code to generate a HTML table based on SQL results. 我使用下面的代码基于SQL结果生成HTML表。

Code

  $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>';
    echo "<td>" . $row['1'] . "</td>";
    echo "<td>" . $row['2'] . "</td>";
    echo "<td>" . $row['3'] . "</td>";
    echo "</tr>";
    unset($row);
  }

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

Question

Is it possible to make the HTML table generation part in the code more dynamic, so that if I need to add an additional column for example I can just ammend the SQL part in the code? 是否可以使代码中的HTML表生成部分更加动态,以便例如在我需要添加其他列的情况下,可以在代码中添加SQL部分?

I had an idea to set the column headings using AS in SQL and I can amend the SQL to use AS to show the real column headings I want for example 我有一个想法,设置使用的列标题AS在SQL,我可以修改使用SQL AS展现真实的列标题我想例如

SELECT orders.order_no    AS "Order No"
,      orders.porder_no   AS "Purchase Order No"
,      orders.date        AS "Date"
,      order_totals.value AS "Total"

but what about the HTML table part, is there some method to just print all columns and rows dynamically, like maybe create a function printTable that would handle any table? 但是HTML表部分呢,有没有一种方法可以动态地打印所有列和行,例如创建一个可以处理任何表的printTable函数?

The $row var is an array so you can loop over that too. $ row var是一个数组,因此您也可以对其进行循环。 Since you want to treat your first field differently write it out before the loop and start the loop at 1. 由于您要对待第一个字段,因此请在循环之前将其写出,并从1开始循环。

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

I don't know what the unset($row) was for, so I left it out. 我不知道unset($ row)的用途,所以我省略了它。

You can use: 您可以使用:

SELECT * FROM {TABLENAME}

Then fetch an array. 然后获取一个数组。 You can get data with $row['{FIELDNAME}'] 您可以使用$row['{FIELDNAME}']获取数据

You can use double loop: 您可以使用双循环:

$results = $PDO->fetchAll(PDO::FETCH_ASSOC); // here is all columns and rows from db query.

echo '<table><tr>';

// loop only first row to get header
foreach ($results[0] as $header => $value) {
   echo "<th>{$header}</th>"
}

echo '</tr>';

// loop all rows
foreach ($results as $row) {
   echo '<tr>';
   // and loop any number of columns
   foreach ($row as $cellName => $cell) {

      // If you have only one special case, than use if statement
      switch ($cellName) {
          case 'order_no':
               echo "<td><a href='http://example.com/getOrder?id={$cell}'>{$cell}</a></td>";
               break;
          default: echo "<td>{$cell}</td>";
      }
   }
   echo '</tr>';
}
echo '</table>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM