需要概念帮助:表格和显示所有行


Concept Help Needed: Tables and Displaying All Rows

>我的网站中有一个上传系统,用户可以在其中上传文件并编写它们的简要说明。 每个上载都有一个相应的 SQL 条目。

然后,我网站上的另一个页面将显示上传的每个文件以及有关该文件的一些特征

问题是:

如何创建具有可变行集的表以及如何设置表以自动填充新行?

有能力使用PHP,但仍然是一个新手,对HTML很弱(我使用所见即所得),并且完全没有Javascript的经验。

任何朝着正确方向的推动都将不胜感激......

为了使用

PHP 使 HTML 表的行"动态",您应该使用 foreach() 控件结构。例如,假设您有一些代码可以从数据库中获取数据,并且它将其作为数组或实现可迭代接口的某种结果语句返回(我们称之为 $results ),然后您可以:

<table>
<thead>
   <tr>
      <th>File Name</th>
      <th>Description</th>
   </tr>
</thead>    
<tbody>
<?php
   foreach($results as $result)
   {
      // Start row
      echo("<tr>");
         echo("<td>" . $result->filename . "</td>");
         echo("<td>" . $result->description . "</td>");
      // End row
      echo("</tr>");
   } 
?>
</tbody>
</table>

可以使用循环根据数据库查询的结果填充表。这是我用于我的一个页面的代码:

$result = $db->query_read("SELECT dice.RollID AS `Roll`, dice.Limit AS `Limit`,
dice.Value AS `Value`, dice.Dateline AS `Dateline`, dice.Comment AS `Comment`
FROM dice
ORDER BY Dateline DESC LIMIT 25");
// ###### MAIN CODE ######
$str = '<table>
<tr><th>ID</th><th>Roll</th><th>Time</th><th>Comment</th></tr>';
while ($resultLoop = $db->fetch_array($result)) {
    $ID = $resultLoop["Roll"];
    $roll = $resultLoop["Value"] . '/' . $resultLoop["Limit"];
    $when = date('m-d-Y H:i:s', $resultLoop["Dateline"]);
    $comment = $resultLoop["Comment"];
    $str .= '<tr>
    <td>' . $ID . '</td>
    <td>' . $roll . '</td>
    <td>' . $when . '</td>
    <td>' . $comment . '</td></tr>';
}
$str .= '</table>';
echo $str;

需要注意的一件事是,我使用$db->*功能,因为它与论坛软件集成在一起。您应该使用此处的mysqli_*函数:http://php.net/manual/en/book.mysqli.php