Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SQL的PHP动态HTML表 背景_Php_Html_Sql - Fatal编程技术网

使用SQL的PHP动态HTML表 背景

使用SQL的PHP动态HTML表 背景,php,html,sql,Php,Html,Sql,我正在使用下面的代码根据SQL结果生成一个HTML表 代码 $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.o

我正在使用下面的代码根据SQL结果生成一个HTML表

代码

  $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>";
但是关于HTML表格部分,是否有一些方法可以动态打印所有列和行,比如创建一个函数
printTable
,它可以处理任何表格?

您可以使用:

SELECT * FROM {TABLENAME}
然后获取一个数组。
您可以使用
$row['{FIELDNAME}']

获取数据,$row变量是一个数组,因此您也可以循环使用它。因为您希望以不同的方式对待第一个字段,所以在循环之前将其写出,并在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>";
}
while($row=oci\u fetch\u数组($stid,oci\u NUM)){
回声“;
回声';
对于($ii=1;$ii<计数($row);$ii++){
回显“$row[$ii]”;
}
回声“;
}

我不知道unset($row)是用来做什么的,所以我省略了它。

您可以使用双循环:

$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>';
$results=$PDO->fetchAll(PDO::FETCH_ASSOC);//下面是db查询中的所有列和行。
回声';
//仅循环第一行以获取标题
foreach($results[0]作为$header=>$value){
回显“{$header}”
}
回声';
//循环所有行
foreach($结果为$行){
回声';
//并循环任意数量的列
foreach($cellName=>$cell的行){
//如果只有一个特殊情况,则使用If语句
交换机($cellName){
“订单号”案例:
回声“;
打破
默认值:echo“{$cell}”;
}
}
回声';
}
回声';

谢谢!是否也可以从SQL中写入列标题?
$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>';