将php多维数组输出到html表
我有一个表单,有8列和一个可变的行数,我需要用一封格式良好的电子邮件发送给客户。表单将所需字段作为多维数组提交。粗略的例子如下:将php多维数组输出到html表,php,html,forms,multidimensional-array,html-table,Php,Html,Forms,Multidimensional Array,Html Table,我有一个表单,有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="
<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" />
根据需要添加行。但是,我找不到任何能够生成这种类型表的示例
看起来这应该是一个相当简单的例子,但我就是找不到一个我也需要的例子。这个怎么样
$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>
$keys=array_keys($_POST['order'][0]);
echo“”。内爆(“,$keys”);
foreach($_POST['order']作为$order){
如果(!is_数组($order))
继续;
echo“”。内爆(“,$order”);
}
回声“
我不久前写的一个表类
<?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;
}
}
?>
要像这样使用:
<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>
在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>
底部
倾斜
装配工
显然,我没有在这里包含所有属性,但希望您能理解。有几件事-首先,看起来您的最后几行PHP中有语法错误-可能您正在查找
echo';}
其次,当您有多行时,哪一列会消失?当您添加更多行时,您会更改订单[1]
在HTML中?第二行、第三行、第四行等的输入名称是什么?哇,我从来没有见过有人使用foreach循环的替代语法。整洁!谢谢!St.John Johnson的解决方案也很有效,但您的代码更容易阅读和调整。为简洁起见,+1-尽管这可能会提供不正确的输出,如果有任何一行的话具有不同/附加列(或者如果键排序不同)@mskfisher很好。您可以使用for循环替换第6行,该循环迭代$keys数组并相应地输出。这将确保输出的顺序正确,并且只包含我们期望的键。如果您对列数比较有信心,还可以调用ksort()
,以确保数组的顺序正确。太棒了!你用这个有效、优秀的结构化代码片段让我开心了!你是否可以发布该示例,使用$keys数组只输出有效的输出?谢谢!
<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>
<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>