Enviar un array multidimensional vía POST con php


Tengo un formulario php que tiene un número conocido de columnas (ej. diámetro superior, diámetro inferior, tela, color, cantidad), pero tiene un número desconocido de filas, ya que los usuarios pueden agregar filas según lo necesiten.

He descubierto cómo tomar cada uno de los campos(columnas) y colocarlos en una matriz propia.

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

Así que lo que termino en el HTML es:

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

Lo que me gustaría hacer ahora es tomar todas las filas y columnas ponerlas en una matriz multidimensional y enviar por correo electrónico el contenido de eso al cliente (preferiblemente en una tabla bien formateada). No he sido capaz de comprender realmente cómo combinar todas esas entradas y selecciones en una buena matriz.

En este punto, voy a tener que intentar usar varios arrays 1D, aunque tengo la idea de que usar un solo array 2D sería una mejor práctica que usar varios arrays 1D.

Author: Willi Mentzel, 2010-03-12

2 answers

Al enviar, obtendrás un array como si se creara así:

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

Sin embargo, sugeriría cambiar los nombres de sus formularios a este formato:

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

Usando ese formato, es mucho más fácil recorrer los valores.

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>';
}
 135
Author: DisgruntledGoat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-03-12 15:44:23

Podría enviar todos los parámetros con dicho nombre:

params[0][topdiameter]
params[0][bottomdiameter]
params[1][topdiameter]
params[1][bottomdiameter]

Luego haces algo como esto:

foreach ($_REQUEST['params'] as $item) {
    echo $item['topdiameter'];
    echo $item['bottomdiameter'];
}
 16
Author: Laimoncijus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-03-12 15:38:43