数字格式的PHP格式表

时间:2015-10-20 18:30:10

标签: php html mysql css

我对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;
}

3 个答案:

答案 0 :(得分:1)

缺少第一行的原因可能是因为您在调用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);

答案 1 :(得分: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...
}

答案 2 :(得分:-1)

制作一个枝条扩展名,返回您想要的字段列表,这样您就可以使用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 %}

这可能适合你。