【问题标题】:Output a php multi-dimensional array to a html table将php多维数组输出到html表
【发布时间】:2011-01-28 17:35:42
【问题描述】:

我有一个包含 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   |

根据需要添加额外的行。但是,我找不到任何可以生成这种类型表的示例!

看起来这应该是相当简单的事情,但我就是找不到一个符合我需要的示例。

【问题讨论】:

  • 有几件事 - 首先看起来你在 PHP 的最后几行有语法错误 - 也许你正在寻找 echo '&lt;/table&gt;'; } 其次,当你有多行时,哪一列消失了?添加更多行时,是否将 HTML 中的 order[0] 更改为 order[1]
  • 第二行、第三行、第四行等输入的名称是什么?

标签: php html forms multidimensional-array html-table


【解决方案1】:

前段时间写的一个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>

【讨论】:

    【解决方案2】:

    在您的 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>
    

    显然,我没有将你的所有属性都包括在内,但希望你能明白。

    【讨论】:

    • 哇,我从未见过有人使用 foreach 循环的替代语法。整洁!
    • 谢谢! St. John Johnson 的解决方案也有效,但您的代码对我来说更易于阅读和调整。
    【解决方案3】:

    这个怎么样?

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

    【讨论】:

    • +1 简洁——尽管如果任何行具有不同/附加列(或者如果键的排序方式不同),这可能会提供不正确的输出。
    • @mskfisher 好点。您可以用 for 循环替换第 6 行,该循环迭代 $keys 数组并相应地输出。这将保证输出的顺序正确,并且只有我们期望的键。
    • 如果您对列数比较有信心,也可以在数组上调用ksort() 以确保其顺序正确。 php.net/manual/en/function.ksort.php
    • 太棒了!你用这个有效的、结构良好的 sn-p 让我度过了愉快的一天!
    • 无论如何您可以发布该示例以仅使用 $keys 数组输出有效输出吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2020-05-27
    • 1970-01-01
    • 2023-03-30
    • 2013-02-28
    相关资源
    最近更新 更多