PHP foreach put together a table

1

I have a page where I receive arrays by the _POST method from a form and I need to create a row with each group of data:

$idfila = $_POST['idfila'];
$destino = $_POST['destino'];
$pedidohs = $_POST['pedidohs'];
$pedido_desarrollo = $_POST['pedido_desarrollo'];
$rendicionhs = $_POST['rendicionhs'];

if (isset($idfila))
{
foreach ($idfila as $clave=>$idfilarecibido)
{
echo '<tr>';
foreach ($destino as $clave=>$destinorecibido)
{
echo '<td>'.$destinorecibido.'</td>';
}
foreach ($pedidohs as $clave=>$pedidohsrecibido)
{
echo '<td>'.$pedidohsrecibido.'</td>';
}
foreach ($pedido_desarrollo as $clave=>$pedido_desarrollorecibido)
{
echo '<td>'.$pedido_desarrollorecibido.'</td>';
}
foreach ($rendicionhs as $clave=>$rendicionhsrecibido)
{
echo '<td>'.$rendicionhsrecibido.'</td>';
}
echo '</tr>';
}
}

I can not assemble a table with each group of data, row by row.

The arrays I receive are variable and the results similar to the following:

array (3) {[0] = > string (1) "1" [1] = > string (1) "2" [2] = > string (1) "3"} array (3) {[1] = > string (12) "Belgrano 361" [2] = > string (9) "Jump 244" [3] = > string (11) "Bulnes 1544"} array (3) {[1] = > string (5) "14:00" [2] = > string (5) "14:30" [3] = > string (5) "17:00"} array (3) {[1] = > string (5) "About" [2] = > string (4) "Payment" [3] = > string (8) "Collection"} array (3) {[1] = > string (5) "16:00" [2] = > string (5) "17:00" [3] = > string (5) "18:00"}

And I need to show it in the following way:

<table>
<tr>
<th>Id</th>
<th>Destino</th>
<th>Hora de Gesti&oacute;n</th>
<th>Desarrollo del tr&aacute;mite</th>
<th>Hora de Rendici&oacute;n</th>
</tr>
<tr>
<td>1</td>
<td>Belgrano 361</td>
<td>14:00</td>
<td>Sobre</td>
<td>16:00</td>
</tr>
<tr>
<td>2</td>
<td>Salta 244</td>
<td>14:30</td>
<td>Pago</td>
<td>17:00</td>
</tr>
<tr>
<td>3</td>
<td>Bulnes 1544</td>
<td>17:00</td>
<td>Cobranza</td>
<td>18:00</td>
</tr>
</table>

Any suggestions? Thank you very much!

    
asked by pointup 11.09.2017 в 16:46
source

1 answer

1

From what I understand, you receive in separate arrays the values of the columns, not the rows.

You should first verify that all the arrays have the same number of elements, but the following that I expose you may not work.

$idfila = $_POST['idfila'];
$destino = $_POST['destino'];
$pedidohs = $_POST['pedidohs'];
$pedido_desarrollo = $_POST['pedido_desarrollo'];
$rendicionhs = $_POST['rendicionhs'];

echo "<table>";
echo "<tr><th>id</th><th>destino</th><th>pedidohs</th><th>pedido_desarrollo</th><th>rendicionhs</th></tr>";
foreach($idfila as $fila) {
    echo "<tr>";
    echo "<td>" . $fila . "</td>";
    echo "<td>" . $destino[$fila] . "</td>";
    echo "<td>" . $pedidohs[$fila] . "</td>";
    echo "<td>" . $pedido_desarrollo[$fila] . "</td>";
    echo "<td>" . $rendicionhs[$fila] . "</td>";
    echo "</tr>">
}

echo "</table>";

This is based on the results of arrays that you have put in your definition. For that reason, I have taken the first array (idFila) that indicates a number and is the one that I use as index in the others, since precisely their indices are those numbers.

    
answered by 12.09.2017 / 15:56
source