baktete miloud baktete miloud - 6 months ago 9
PHP Question

Send array from page to another

table result vardumb result

is there anyway to display my data to fit correctly on cells ?

page1.php

<td><input type='hidden' name='nom[]' value='".$row['nom']." ".$row['prenom']."'/></td>


page2.php

$idArrays = array($_POST['nom'], $_POST['montant'], $_POST['disponible_fiche'],$_POST['date_f'],$_POST['observation'] );


foreach ($idArrays as $idArray) {

echo '<tr>';
foreach ($idArray as $key ) {

echo '<td>'.$key.'</td>';

}
echo '</tr>';

}

Answer

If you make your $idArray a little bit different it works. If all the arrays have the same length you can use array_map to create a new array in the right format.

$idArrays = array_map(null,$_POST['nom'], $_POST['montant'], $_POST['disponible_fiche'],$_POST['date_f'],$_POST['observation'] );

If some of the arrays are shorter, they will be padded with nulls to the length of the longest.

You can use your normal foreach then.

foreach ($idArrays as $idArray) {
    echo '<tr>';
    foreach ($idArray as $key ) {
        echo '<td>'.$key.'</td>';

    }
    echo '</tr>';
 }

Explanation:

This method "zips" the arrays together, rather than making an array of the original arrays.

Example:

<?php
$a = array("1","2");
$b = array("a","b");
$c = array("!","§");
print_r(array_map(null,$a,$b,$c));
print_r(array($a,$b,$c));
?>

Outputs:

with array_map:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => a
            [2] => !
        )

    [1] => Array
        (
            [0] => 2
            [1] => b
            [2] => §
        )

)

with normal array declaration:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
        )

    [1] => Array
        (
            [0] => a
            [1] => b
        )

    [2] => Array
        (
            [0] => !
            [1] => §
        )

)