将php多维数组输出到html表 [英] Output a php multi-dimensional array to a html table

查看:102
本文介绍了将php多维数组输出到html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含8列和可变行数的表单,我需要通过格式良好的电子邮件将其发送给客户端。表单将所需字段作为多维数组提交。粗略的例子如下:

 < input name =order [0] [topdiameter]type =textid = topdiameter0value =1size =5/> 
< input name =order [0] [bottomdiameter]type =textid =bottomdiameter0value =1size =5/>
< input name =order [0] [slantheight]type =textid =slantheight0value =1size =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 =textid =washerdrop0value =1size =5/>
< select name =order [0] [fabrictype]id =fabrictype>
< option value =linen> Linen< / option>
< option value =pleated> Pleated< / option>
< / select>
< select name =order [0] [colors]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 =textid =quantity0value =1size =5/>

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



这就是我现在使用的(从网上抓取)。<如果(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行或更多行,则其中一列会消失。我希望将表格格式化为:

  |顶部|底部|倾斜|钳工|垫圈|面料|颜色|数量| 
---------------------------------------------- --------------------------
| value |价值|价值|价值|价值|价值|价值|价值|

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



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

解决方案

在您的HTML中,尝试以下内容:

 <表> 
< tr>
< th> Bottom< / th>
< th> Slant< / th>
< th> Fitter< / th>
< / tr>
<?php foreach($ _POST ['order']作为$ order):?>
< tr>
< td><?php echo $ order ['bottomdiameter']?>< / td>
< td><?php echo $ order ['slantheight']?>< / td>
< td><?php echo $ order ['fittertype']?>< / td>
< / tr>
<?php endforeach; ?>
< / table>

显然,我没有在那里包含你所有的属性,但希望你明白了。


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:

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

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

解决方案

In your HTML, try something like this:

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

这篇关于将php多维数组输出到html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆