php - 如何在表格中添加标题,以及如何在 php 中添加边框

标签 php postgresql formatting multiple-columns

我一直在编写查询以从 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";

最佳答案

试试这个:

//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; 

关于php - 如何在表格中添加标题,以及如何在 php 中添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25923049/

相关文章:

php - 使用 php-github-api 的 Github 身份验证

oracle - 在 SQLPlus 中格式化查询的输出

PHP 比较 2 个数组的值是否存在

php - 将单选按钮中的两个变量插入数据库

php - 包含的 PHP 文件中的变量信息

django - 由于 gdal 库,heroku push 被拒绝

python - Django on Production 不在 Django Admin 中显示我模型的所有字段

sql - 需要 SQL 优化(也许 DISTINCT ON 是原因?)

Winforms RichtextBox 粗体/斜体/下划线格式问题

c++ - 如何以漂亮的格式放置不同的 C 字符串?