MrAxlee MrAxlee - 2 months ago 8x
PHP Question

How do I pass multi-dimensional arrays through a form in PHP?

I've created a table where the user can add both columns and rows, so both of these are unknown lengths. All the fields for these contain input boxes for the user to enter data. (ignore the x's, they're links to related pages).

PHP for the table (line 46 is where I'm passing the array)

I've decided the best way to pass these for input into a mysql database is through a multi-dimensional array. I've heard I can do so, but I can't find any related documentation for the life of me.

When the user clicks submit, all the data should be inputted into a table like so:

(where header = cem_param_id and product (left side) = cem_prod_id)

How do I go about doing this?

I am aware MySQL is outdated. I'm still learning to update it.


I passed the name from th inputs as the array;

<input type='checkbox' name='prod_matrix[".$ceprod_id."][".$ceparam_id."]'>

where $ceprod_id is the product ID and $ceparam_id is the parameter ID (the headers)

I then caught these using 2 foreach loops:

$prod_matrix = $_POST['prod_matrix'];
    foreach($prod_matrix as $prodid => $product)
        echo $prodid;
        foreach ($product as $paramid => $value)
            echo $paramid;
            echo $value;
        echo "<br>";

Where $value is the data entered into the field (checkboxes give a value of "on" if checked, and pass nothing at all if unchecked)

I then used this information to build the query.