简体   繁体   English

如何从SQL用PHP创建动态表

[英]How to create a dynamic table in PHP from SQL

I try to make a dynamic table. 我尝试制作一个动态表。 The data comes from a database. 数据来自数据库。 It works so far, but I want to make a table with seperate fields, not like my way. 到目前为止,它仍然有效,但是我想用不同的字段创建一个表,而不是像我这样。 My code so far: 到目前为止,我的代码:

 <?php

$result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
$num_rows = mysql_num_rows($result); 
echo("Buy Date"."|Expire Date");
echo "<br />";
echo "<br />";
while ($row = mysql_fetch_array($result)) {
    echo '<th>'.$row['buyTime'].'</th>'."|".'<th>'.$row['untilTime'].'</th>'; 
    echo "<br />";
}
?>

The result: 结果:

Click here 点击这里

So how can I make a correct table, not a pseudo one? 那么,如何制作正确的表而不是伪表呢?

Thank you :) 谢谢 :)

Regards 问候

Use a table element on html and put your php loop code inside the tbody element. 在html上使用表格元素,然后将您的php循环代码放入tbody元素中。 How to properly create a table; 如何正确创建表;

<table>
 <thead>
  <tr>
   <th>Buy Date</th>
   <th>Expire Date</th>
  </tr>
 </thead>

 <tbody>
 <?php
  $result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
  $num_rows = mysql_num_rows($result);

  while ($row = mysql_fetch_array($result)) {
   echo '<tr>';
   echo '<td>'.$row['buyTime'].'</td><td>'.$row['untilTime'].'</td>';
   echo '</tr>'
  }
 ?>
 </tbody>
</table>
    <!DOCTYPE html>
    <html>
    <head>
    <style>
    table {
        border-collapse: collapse;
        width: 100%;
    }

    th, td {
        text-align: left;
        padding: 8px;
    }

    tr:nth-child(even){background-color: #f2f2f2}

    th {
        background-color: #4CAF50;
        color: white;
    }
    </style>
    </head>
    <body>

    <h2>Colored Table Header</h2>

    <table>
      <tr>
        <th>Buy Date</th>
        <th>Expire Date</th>
      </tr>

      <?php
$result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
$num_rows = mysql_num_rows($result); 

while ($row = mysql_fetch_array($result)) {  ?>
      <tr>

        <td><?php echo $row['buyTime']; ?></td>
        <td><?php echo $row['untilTime']; ?></td>

      </tr>
<?php } ?>


    </table>

    </body>
    </html>

NOTE: An HTML table is defined with the <table> tag. 注意:HTML表是用<table>标记定义的。

Each table row is defined with the <tr> tag. 每个表行都用<tr>标记定义。 A table header is defined with the <th> tag. 表头是用<th>标记定义的。 By default, table headings are bold and centered. 默认情况下,表标题为粗体和居中。 A table data/cell is defined with the <td> tag. 表数据/单元格用<td>标记定义。 Moreover Mysql is outmoded please try learn new approach. 而且Mysql已过时,请尝试学习新方法。 You can learn it on w3schools php 您可以在w3schools php上学习它

Good Luck!!! 祝好运!!!

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

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