【问题标题】:How to add a header to a table, and how to add a border in php如何在表格中添加表头,以及如何在php中添加边框
【发布时间】:2014-09-18 21:56:10
【问题描述】:

我一直在编写查询以从 php 中的表中获取信息。我可以打印出信息,但它看起来一点也不漂亮。我想知道如何通过将黑线作为每个单元格的边框来使输出的表格更好,以及如何在表格中添加列标题。现在我的第一个查询的输出如下所示:

Massachusetts   152082
Missouri    151580
Illinois    111454

我希望我的输出看起来像这样(我还希望每个单元格都有一个黑色边框):

district    population
Massachusetts   152082
Missouri    151580
Illinois    111454

这是我打印表格时的代码。我认为您不需要查询中的任何代码,所以我不会发布。提前感谢您的帮助。

 echo "<table>\n";
                    while($line = pg_fetch_array($result, null, PGSQL_ASSOC)){
                            echo "\t<tr>\n";
                            foreach($line as $col_value){
                                    echo "\t\t<td>$col_value</td>\n";
                            }

                    echo "\t</tr>\n";
            }
            echo "</table>\n";

【问题讨论】:

    标签: php postgresql formatting multiple-columns


    【解决方案1】:

    试试这个:

    //table header
    $table = "<table border='1px'>";
    $table .= "<thead>";
    $table .= "<tr>";
    $i = pg_num_fields($result);
    for ($j = 0; $j < $i; $j++) {
        $fieldname = pg_field_name($result, $j);
        $table .= "<th>$fieldname</th>"; 
    }
    $table .= "</tr>";
    //table body
    $table .= "<tbody>";  
    while($row = pg_fetch_assoc($result))
    {
        $table .= "<tr>";
        foreach ($row as $key => $value)
        {
            $table .= "<td>$value</td>";
        }
        $table .= "</tr>";  
    }
    $table .= "</tbody>";
    $table .= "</table>";
    //echo table
    echo $table; 
    

    【讨论】:

    • 我用我拥有的代码切换了它,现在它输出数字 0
    • 我将如何编写一个告诉用户返回多少行的语句?
    • 使用pg_num_rows($result);
    • 就像 echo "The Query returned" 。 pg_num_rows($result)."lines";
    猜你喜欢
    • 1970-01-01
    • 2011-05-04
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多