tags:

views:

44

answers:

1

Hi!

I have 15 input fields, each one is in its own table cell. They are in the table because I need them to look like a matrix.

The question is now - when an user enters data in those input fields and hits submit, how do I transfer that data into a matrix?

The real problem is that from that input data I need to find min values in each row and max values in each column of the original table.

I hope I was clear enough.

Thanks!

+4  A: 

Use arrays. So for a two dimensional matrix:

<input type="text" name="matrix[0][0]" value="cell_0_0"> // The top left element
<input type="text" name="matrix[0][1]" value="cell_0_1"> // The top 2nd element
...
<input type="text" name="matrix[1][0]" value="cell_1_0"> // The 2nd left element

Then, in PHP, all you need to do is

$matrix = $_POST['matrix'];

$matrix would then be:

$matrix = array(
    "0" => array(
        "0" => "cell_0_0",
        "1" => "cell_0_1",
    ),
    "1" => array(
        "0" => "cell_1_0",
        "1" => "cell_1_1",
    ),
)

EDIT: To generate an array with width $i and height $j: (It will also "fill out" an existing matrix)

$matrix = array();
for ($a = 0; $a < $j; $a++) {
    if (!isset($matrix[$a])) {
        $matrix[$a] = array();
    }
    for ($b = 0; $b < $i; $b++) {
        if (!isset($matrix[$a][$b])) {
            $matrix[$a][$b] = 'start_value';
        }
    }
}

Then, to get the value at any point:

$val = $matrix[1][2];

And to set the value at any point (once defined):

$matrix[1][2] = $val;
ircmaxell
This was very useful :)But I have variable number of fields, and matrix could be anything from 1-1 to M-M elements.My input fields are named field_$i,$j, where i,j are positions of input elements in the original table. I did that with for counter.I guess it would be something like$matrix = array( "$i" => array( "what_here? :)" => ) "$j" => array....
Nikola
@Nikola - I edited my original answer... Hopefully that helps
ircmaxell
It was, a bit, but not too much though :/But, as I still think that I was unclear with my problem, I set up a nice picture showing what I really need :)http://img46.imageshack.us/img46/350/matrixu.gif
Nikola