开发者

Putting data from the input field into the matrix

开发者 https://www.devze.com 2023-01-01 08:53 出处:网络
I have 15 input fields, each one is in its own table cell. They are in the table because I n开发者_如何学运维eed them to look like a matrix.

I have 15 input fields, each one is in its own table cell. They are in the table because I n开发者_如何学运维eed 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.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消