[英]PHP format table in number format
我对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;
}
第一行丢失的原因可能是因为您正在调用mysqli_fetch_assoc($res);
在那之后的mysqli_fetch_row($res)
而不重置迭代器。
您可以通过放置mysqli_free_result($result);
来防止这种情况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;
}
希望这可以帮助。
编辑:正如马克B所说。 最好使一个循环而不是两个循环。 这样,您不必调用mysqli_free_result($result);
无论是。
您正在浪费/扔掉第一行:
$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...
}
做一个树枝扩展名,返回想要的字段列表,这样您就可以使用php来获取字段。 之后,使用树枝的属性功能
{{attribute(object,fields)}}调用对象上的吸气剂
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 %}
这可能对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.