How to pass a table through a form PHP

Viewed 37

I need to code a web application and I need to pass a two-dimensional table through a form. So in my form, I have 2 loops for like this:

for($col = 0; $col<$SizeM; $col++){
    echo "<tr>" ;
    for($line = 0; $line<$SizeM; $line++){
        echo"<td><input type='number' name=m[$col][$line] min='0' max='1' size = '1'></td>" ;
    }
    echo "</tr>" ;
}

and I don't know how to get this table back...

I'm trying to do this but it's not working.

$m = $_GET['m] ;
echo "$m [0][1]";

but when I do print_r($m); he works and tells me this

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

so how can I use this table?

3 Answers

use this :

 echo '<td><input type="number" name="m['.$col.']['.$line.']" min="0" max="1" size = "1"></td>' ;

When printing the value of a multidimensional array inside a string, you must wrap it with brackets, otherwise PHP will only take the first index and asume all that follows is text:

$m = [[1,2,3], [4,5,6]];

echo "{$m[0][1]}";
// outputs 2, desired value

echo "$m[0][1]";
// outputs "Array[1]" (Array = $m's type, "[1]" string literal)

echo "$m [0][1]";
// outputs "Array [0][1]", none of the indexes
// are taken into account, [0][1] are printed as strings

You can try this:

foreach ($_GET['m'] as $key => $value) {
echo "<tr>";
for ($i = 0; $i < count($value); $i++) {
    echo "<td>" . $value[$i] . "</td>";
}
echo "<tr>";

}

Related