【问题标题】:Concept Help Needed: Tables and Displaying All Rows需要概念帮助:表格和显示所有行
【发布时间】:2012-05-28 17:42:42
【问题描述】:

我的站点中有一个上传系统,用户可以在其中上传文件并对其进行简要说明。每个上传都有一个对应的 SQL 条目。

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

问题是:

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

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

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

【问题讨论】:

  • 如果你了解html表格的结构,问题出在哪里?

标签: php html tabular


【解决方案1】:

为了使用 PHP 使 HTML 表格的行“动态”,您应该使用foreach() 控制结构。例如,假设您有一些代码从数据库中获取数据并将其作为数组或某种实现 Iterable 接口的结果语句返回(我们将其称为$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>

【讨论】:

    【解决方案2】:

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

    $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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多