MrAxlee
MrAxlee

Reputation: 155

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). Table

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) Database

How do I go about doing this?

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

Upvotes: 0

Views: 7465

Answers (3)

Boban Mikičić
Boban Mikičić

Reputation: 1

Here is a solution:

Submitting a multidimensional array via POST with php

On submitting, you would get an array as if created like this:

 $_POST['topdiameter'] = array( 'first value', 'second value' );
    $_POST['bottomdiameter'] = array( 'first value', 'second value' );

However, I would suggest changing your form names to this format instead:

  name="diameters[0][top]"
   name="diameters[0][bottom]"
   name="diameters[1][top]"
   name="diameters[1][bottom]"
   ...

Using that format, it's much easier to loop through the values.

if ( isset( $_POST['diameters'] ) )
{
    echo '<table>';
    foreach ( $_POST['diameters'] as $diam )
    {
        // here you have access to $diam['top'] and $diam['bottom']
        echo '<tr>';
        echo '  <td>', $diam['top'], '</td>';
        echo '  <td>', $diam['bottom'], '</td>';
        echo '</tr>';
    }
    echo '</table>';
}

Upvotes: 0

MrAxlee
MrAxlee

Reputation: 155

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.

Upvotes: 0

Anto S
Anto S

Reputation: 2449

Since we can't find solution in single step and to make myself clear are you having a form like below:

<?php
    $dbCon = new PDO("mysql:host=localhost;database=test", 'root', '');
    if(isset($_POST) && !empty($_POST)) {
        echo '<pre>'; print_r($_POST);
        die;
    }

?>

HTMl View:

    <!DOCTYPE html>
<html>
<title>Stack HTML</title>
<link rel="stylesheet" href="../repo/css/bootstrap.css" type="text/css" />
<script src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script src="../repo/js/bootstrap.js"></script>
<script src="../repo/js/jquery.validate.js"></script>
<head>
</head>
<body>
    <div class="container">
        <form method="post">
            <table class="table">
                <tr>
                    <td><input type="checkbox" name="row[1][1]"  /></td>
                    <td><input type="checkbox" name="row[1][2]"  /></td>
                    <td><input type="checkbox" name="row[1][3]"  /></td>
                </tr>
            </table>
            <input type="submit" name="submit" value="Submit" class="btn btn-primary" />
            <input type="button" name="submit" value="Add Horizontal" class="btn btn-success" />
            <input type="button" name="submit" value="Add Vertical" class="btn btn-info" />
        </form>
    </div>
<script>
    $(function(){
        $(document).on('click', '.btn-success', function(){
            var tableColumns = $('.table tr:eq(0)>td').length;
            var tableRow = $('.table tr').length;
            var NewRowNumber = tableRow+1; 

            var htmlElement = '<tr>';
            for(i=1; i<=tableColumns; i++){
                htmlElement += '<td><input type="checkbox" name="row['+NewRowNumber+']['+i+']"  /></td>';
            }
            htmlElement += '</tr>';
            $('.table').append(htmlElement);
        });
        $(document).on('click', '.btn-info', function(){
                console.log($(this))
            var RowCount = 1;
            $('.table tr').each(function() {
                var Column = $(this).find('td').length;
                $(this).append('<td><input type="checkbox" name="row['+RowCount+']['+Column+']"  /></td>')
                RowCount++;
            });
        });
    })
</script>   
</body>
</html>

In addition with you will have dynamic row also?

Upvotes: 1

Related Questions