【问题标题】:PHP format table in number format数字格式的PHP格式表
【发布时间】:2016-01-19 13:21:25
【问题描述】:

我对 PHP 很陌生,所以请原谅我提出这个简单的问题,但我无法正确格式化一个简单的表格。

我使用 PHP 从 MYSQL 获取结果并将其显示在 HTML 页面中。表格左侧和顶部有 ID 标签,正文是数字。

这是 PHP 代码:

$sql = "CALL pivot('DATA', 'amount', 'scen_id', 'dim2_id', 'where ent_id=''abc1''')";
$res = mysqli_query($mysqli, $sql) or die('Query failed: ' . mysqli_error($mysqli));

$row = mysqli_fetch_assoc($res);

echo '<table>';

// printing table header
foreach($row as $name => $value) {
    echo "<th>$name</th>";
}
// printing table rows
while($row = mysqli_fetch_row($res))
{
    echo "<tr>";

    foreach($row as $cell) 
        echo "<td>$cell</td>\n";

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

这是我得到的:

scen_id ProdA       ProdB       ProdC       Total
BUD     59903423    66278016    55422083    181603522
FORC    37195845    52753807    41339980    131289632
Total   157331578   185154736   164437401   506923715

虽然这比空白页要好,但缺少数据的第一行(包含 ACT... 等)。当我在 Mysql 中运行查询时,它确实出现了。我希望一切都正确对齐,并且数字用千位分隔符格式化。我知道您不能为此使用 CSS。

我已经尝试过任何东西,比如这个:

echo "<td>".number_format($cell)."</td>\n";

但是结果是这样的:

scen_id ProdA       ProdB       ProdC       Total
        59,903,423  66,278,016  55,422,083  181,603,522
        37,195,845  52,753,807  41,339,980  131,289,632
        157,331,578 185,154,736 164,437,401 506,923,715

没有更多的行标签......而且仍然没有正确对齐。仍然缺少第一行。我希望有人能告诉我如何让它工作。我想这是一项标准任务,没有人会问这个问题。我在网上找不到太多内容。

编辑:好的问题解决了,谢谢大家!为了完整起见:这是我的工作代码;

$sql="CALL pivot('DATA', 'amount', 'scen_id', 'dim2_id', 'where ent_id=''abc1''')";
$res=mysqli_query($mysqli, $sql) or die('Query failed: ' . mysqli_error($mysqli));

echo '<table>';

$first = true;
while($row = mysqli_fetch_assoc($res)) {
   if ($first) {
       foreach(array_keys($row) as $name) {
            echo "<th>$name</th>";
       }
       $first = false;
   }
    echo "<tr>";
        foreach($row as $cell) 
        if (!is_numeric($cell)) 
            {echo "<td>$cell</td>\n";} 
        else 
            {echo "<td>".number_format($cell)."</td>\n";}

    echo "</tr>\n";
}

echo '</table>';

?>

这里是 CSS 代码:

table tr td, table tr th {
    text-align: right;
    font-size: 10px;
    font-family: Verdana, 'Lucida Grande', Helvetica, Arial, Sans-Serif;
    padding: 0 4px;
}

【问题讨论】:

  • 你取出一行然后把它扔掉,然后进入你的 while() 循环。一旦你 fetch()ed 一行,它就消失了。

标签: php html mysql css


【解决方案1】:

您正在浪费/丢弃第一行:

    $row = mysqli_fetch_assoc($res);  <---fetch first row

foreach($row as $name => $value) {  <--- spit out column names, throw away row

while($row = mysqli_fetch_row($res)) <---fetch/display REMAINING rows

一个更好/可行的解决方案更像是

$res = mysqli_query(...) or die(mysqli_error(...));
$first = true;
while($row = mysqli_fetch_assoc()) {
   if ($first) {
       foreach(array_keys($row) as $name) {
            ... display field names
       }
       $first = false;
   }
   ... display row data as usual...
}

【讨论】:

  • 哇,感谢您的快速回答。我将在今天晚些时候试一试,然后告诉你。
  • 谢谢马克。我欠你一个人情。桌子现在看起来和我想要的完全一样。
  • 有效!我自己不会发现的。现在格式也固定了,在 CSS 中我添加了一些填充和字体魔法让它看起来非常漂亮。
【解决方案2】:

缺少第一行的原因可能是因为您在调用mysqli_fetch_assoc($res); 之后调用mysqli_fetch_row($res) 而不重置迭代器。

您可以通过在两者之间添加mysqli_free_result($result); 来防止这种情况发生。

不包括我建议检查$cell 变量是否为数字的行标签。你可以这样做

if (!is_numeric($cell)) {
  continue; // Go to next item in array
}

// Your code

关于右对齐文本。你可以用 css 做到这一点:

table tr td, table tr th {
    text-align: right;
}

希望这会有所帮助。

编辑:正如 Marc B 所说。最好做一个循环而不是两个。这样你也不必打电话给mysqli_free_result($result);

【讨论】:

  • 谢谢科林。我会试试,让你知道
  • 问题解决了!谢谢 Colin,数字测试效果很好。
【解决方案3】:

制作一个 Twig 扩展,返回您想要的字段列表,这样您就可以使用 php 来获取字段。之后使用twig的属性函数

{{ attribute(object, fields) }} 调用对象上的 getter

http://twig.sensiolabs.org/doc/functions/attribute.html

{% set temp = entities|first %}
{% set fields = getObjectFields(temp) %}
<tr>
{% for property_title in fields %} 
    <td>{{ property_title }}</td>
{% endfor %}
</tr>
{% for item in entities %}
    <tr>
        {% for field in fields %}
            <td>{{ attribute(item, field) }}</td>
        {% endfor %}
    </tr>
{% endfor %}

这可能对你有用。

【讨论】:

  • 这与 twig 完全 NOTHING 有关系,告诉 OP 安装一些其他软件来解决问题不是解决方案。
  • 谢谢西卡尔。我会阅读有关 twigs 的信息并尝试您的解决方案
  • 好的,我阅读了链接的网站。我会记住的。现在普通的 PHP / HTML / CSS 解决方案效果很好。
猜你喜欢
  • 2016-10-29
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 1970-01-01
相关资源
最近更新 更多