php 输出一个php多维数组到一个html表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2465046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Output a php multi-dimensional array to a html table
提问by Fireflight
I have a form that has 8 columns and a variable number of rows which I need to email to the client in a nicely formatted email. The form submits the needed fields as a multidimensional array. Rough example is below:
我有一个包含 8 列和可变行数的表单,我需要以格式良好的电子邮件将其通过电子邮件发送给客户。该表单将所需字段作为多维数组提交。粗略的例子如下:
<input name="order[0][topdiameter]" type="text" id="topdiameter0" value="1" size="5" />
<input name="order[0][bottomdiameter]" type="text" id="bottomdiameter0" value="1" size="5" />
<input name="order[0][slantheight]" type="text" id="slantheight0" value="1" size="5" />
<select name="order[0][fittertype]" id="fittertype0">
<option value="harp">Harp</option>
<option value="euro">Euro</option>
<option value="bulbclip">Regular</option>
</select>
<input name="order[0][washerdrop]" type="text" id="washerdrop0" value="1" size="5" />
<select name="order[0][fabrictype]" id="fabrictype">
<option value="linen">Linen</option>
<option value="pleated">Pleated</option>
</select>
<select name="order[0][colours]" id="colours0">
<option value="beige">Beige</option>
<option value="white">White</option>
<option value="eggshell">Eggshell</option>
<option value="parchment">Parchment</option>
</select>
<input name="order[0][quantity]" type="text" id="quantity0" value="1" size="5" />
This form is formatted in a table, and rows can be added to it dynamically. What I've been unable to do is get a properly formatted table out of the array.
此表单在表格中格式化,并且可以动态地向其中添加行。我一直无法做的是从数组中获取格式正确的表。
This is what I'm using now (grabbed from the net).
这就是我现在正在使用的(从网上抓取的)。
<?php
if (isset($_POST["submit"])) {
$arr= $_POST['order']
echo '<table>';
foreach($arr as $arrs)
{
echo '<tr>';
foreach($arrs as $item)
{
echo "<td>$item</td>";
}
echo '</tr>';
}
echo '</table>;
};
?>
This works perfectly for a single row of data. If I try submitting 2 or more rows from the form then one of the columns disappears. I'd like the table to be formatted as:
这对于单行数据非常有效。如果我尝试从表单提交 2 行或更多行,那么其中一列就会消失。我希望表格的格式为:
| top | Bottom | Slant | Fitter | Washer | Fabric | Colours | Quantity |
------------------------------------------------------------------------
|value| value | value | value | value | value | value | value |
with additional rows as needed. But, I can't find any examples that will generate that type of table!
根据需要添加额外的行。但是,我找不到任何可以生成这种类型表的示例!
It seems like this should be something fairly straightforward, but I just can't locate an example that works the way I need it too.
看起来这应该是相当简单的事情,但我只是找不到一个可以按照我需要的方式工作的示例。
回答by St. John Johnson
How about this?
这个怎么样?
$keys = array_keys($_POST['order'][0]);
echo "<table><tr><th>".implode("</th><th>", $keys)."</th></tr>";
foreach ($_POST['order'] as $order) {
if (!is_array($order))
continue;
echo "<tr><td>".implode("</td><td>", $order )."</td></tr>";
}
echo "</table>
回答by Powertieke
A Table class I wrote some time ago
前段时间写的一个Table类
<?php
class Table {
protected $opentable = "\n<table cellspacing=\"0\" cellpadding=\"0\">\n";
protected $closetable = "</table>\n";
protected $openrow = "\t<tr>\n";
protected $closerow = "\t</tr>\n";
function __construct($data) {
$this->string = $this->opentable;
foreach ($data as $row) {
$this->string .= $this->buildrow($row);
}
$this->string .= $this->closetable;
}
function addfield($field, $style = "null") {
if ($style == "null") {
$html = "\t\t<td>" . $field . "</td>\n";
} else {
$html = "\t\t<td class=\"" . $style . "\">" . $field . "</td>\n";
}
return $html;
}
function buildrow($row) {
$html .= $this->openrow;
foreach ($row as $field) {
$html .= $this->addfield($field);
}
$html .= $this->closerow;
return $html;
}
function draw() {
echo $this->string;
}
}
?>
To be used like this :
像这样使用:
<body>
<?php
$multiDimArray = []; # Turn the form array into a matrix
for ($i = 0; $i < count($_POST['order']); $i++) {
$multiDimArray[] = [];
foreach ($_POST['order'][$i] as $key=>$value) {
if ($i == 0) {
$multiDimArray[$i][] = $key;
}
$multiDimArray[$i][] = $value;
}
}
$table = new Table($multiDimArray); # Create and draw the table
$table->draw();
?>
</body>
回答by Jason Swett
In your HTML, try something like this:
在您的 HTML 中,尝试如下操作:
<table>
<tr>
<th>Bottom</th>
<th>Slant</th>
<th>Fitter</th>
</tr>
<?php foreach ($_POST['order'] as $order): ?>
<tr>
<td><?php echo $order['bottomdiameter'] ?></td>
<td><?php echo $order['slantheight'] ?></td>
<td><?php echo $order['fittertype'] ?></td>
</tr>
<?php endforeach; ?>
</table>
Obviously, I'm not including all your attributes there, but hopefully you get the idea.
显然,我没有把你的所有属性都包括在内,但希望你能明白。

