繁体   English   中英

将php多维数组输出到html表

[英]Output a php multi-dimensional array to a 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="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" />

此表单以表格格式化,并且可以动态地向其添加行。 我无法做的是从数组中获取格式正确的表。

这就是我现在正在使用的东西(从网上抓起来)。

<?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>;
};
?>

这适用于单行数据。 如果我尝试从表单中提交2行或更多行,则其中一列会消失。 我希望将表格格式化为:

| top | Bottom | Slant | Fitter | Washer | Fabric | Colours | Quantity |
------------------------------------------------------------------------
|value| value  | value | value  | value  | value  |  value  |  value   |

根据需要添加其他行。 但是,我找不到任何会产生这种类型表的例子!

看起来这应该是相当简单的事情,但我找不到一个按照我需要的方式工作的例子。

这个怎么样?

$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>

我上次写的一堂课

<?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>

显然,我没有在那里包含你所有的属性,但希望你能得到这个想法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM