使用php通过POST提交多维数组 [英] Submitting a multidimensional array via POST with php

查看:547
本文介绍了使用php通过POST提交多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已知数量的列(例如顶部直径,底部直径,结构,颜色,数量)的php表单,但行数未知,因为用户可以根据需要添加行。



我已经发现如何把每个字段(列)放到他们自己的数组中。

 < input name =topdiameter ['+ current +']type =textid =topdiameter'+ current +'size =5/> 
< input name =bottomdiameter ['+ current +']type =textid =bottomdiameter'+ current +'size =5/>

所以我最终在HTML中是:

 < tr> 
< td>< input name =topdiameter [0]type =textid =topdiameter0size =5/>< / td>
< td>< input name =bottomdiameter [0]type =textid =bottomdiameter0size =5/>< / td>
< / tr>
< tr>
< td>< input name =topdiameter [1]type =textid =topdiameter1size =5/>< / td>
< td>< input name =bottomdiameter [1]type =textid =bottomdiameter1size =5/>< / td>
< / tr>

...等等。

现在我想要做的是将所有行和列放入多维数组中通过电子邮件将内容发送给客户端(最好是格式良好的表格)。我一直无法真正理解如何将所有这些输入和选择组合到一个漂亮的数组中。



此时,我将不得不尝试使用几个一维数组,虽然我认为使用单个二维数组比使用多个一维数组更好。

解决方案

  $ _ POST ['topdiameter'] = array(p> 0)在提交时,您会得到一个数组,如下所示: '第一值','第二值'); 
$ _POST ['bottomdiameter'] = array('first value','second value');

但是,我建议将表单名称改为这种格式:

  name =diameters [0] [top]
name =diameters [0] [bottom]
name =直径[1] [顶部]
名称=直径[1] [底部]
...

使用这种格式,循环访问值会更容易。

  if(isset $ _POST ['diameters']))
{
echo'< table>';
foreach($ _POST ['diameters'] as $ diam)
{
//在这里您可以访问$ diam ['top']和$ diam ['bottom']
echo'< tr>';
echo'< td>',$ diam ['top'],'< / td>';
echo'< td>',$ diam ['bottom'],'< / td>';
echo'< / tr>';
}
echo'< / table>';
}


I have a php form that has a known number of columns (ex. top diameter, bottom diameter, fabric, colour, quantity), but has an unknown number of rows, as users can add rows as they need.

I've discovered how to take each of the fields(columns) and place them into an array of their own.

<input name="topdiameter['+current+']" type="text" id="topdiameter'+current+'" size="5" />
<input name="bottomdiameter['+current+']" type="text" id="bottomdiameter'+current+'" size="5" />

So what I end up with in the HTML is:

<tr>
  <td><input name="topdiameter[0]" type="text" id="topdiameter0" size="5" /></td>
  <td><input name="bottomdiameter[0]" type="text" id="bottomdiameter0" size="5" /></td>
</tr>
<tr>
  <td><input name="topdiameter[1]" type="text" id="topdiameter1" size="5" /></td>
  <td><input name="bottomdiameter[1]" type="text" id="bottomdiameter1" size="5" /></td>
</tr>

...and so on.

What I would like to do now is take all the rows and columns put them into a multidimensional array and email the contents of that to the client (preferably in a nicely formatted table). I haven't been able to really comprehend how to combine all those inputs and selects into a nice array.

At this point, I'm going to have to try to use several 1D arrays, although I have the idea that using a single 2D array would be a better practice than using several 1D arrays.

解决方案

On submitting, you would get an array as if created like this:

$_POST['topdiameter'] = array( 'first value', 'second value' );
$_POST['bottomdiameter'] = array( 'first value', 'second value' );

However, I would suggest changing your form names to this format instead:

name="diameters[0][top]"
name="diameters[0][bottom]"
name="diameters[1][top]"
name="diameters[1][bottom]"
...

Using that format, it's much easier to loop through the values.

if ( isset( $_POST['diameters'] ) )
{
    echo '<table>';
    foreach ( $_POST['diameters'] as $diam )
    {
        // here you have access to $diam['top'] and $diam['bottom']
        echo '<tr>';
        echo '  <td>', $diam['top'], '</td>';
        echo '  <td>', $diam['bottom'], '</td>';
        echo '</tr>';
    }
    echo '</table>';
}

这篇关于使用php通过POST提交多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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