0

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.

MrAxlee
  • 155
  • 1
  • 10
  • Uhm, MySql is not "outdated" anyway, where did you heard this? (might be it's me who's on the wrong side, mind you) – Damien Pirsy Apr 13 '15 at 10:25
  • @DamienPirsy Not outdated as such, it's deprecated. Should be using PDO or MySQLi instead – MrAxlee Apr 13 '15 at 10:28
  • @MrAxlee Dynamic row or columns will have text box or checkbox? – Anto S Apr 13 '15 at 10:36
  • @imaphpdeveloper When the user adds a new column (parameter) they pick "text" or "checkbox", which is added to the database under a different table. When the table is printed out it just does etc – MrAxlee Apr 13 '15 at 10:41
  • You're passing the arrays correctly, so the problem must reside in the code you use to catch the arrays and actually put them into your database. – BudwiseЯ Apr 13 '15 at 10:55
  • If you need to pass multi-dimensional data to the server, you almost certainly want to be sending over JSON which would handle that perfectly and you can just use ```json_decode($content,true)``` to get your array in PHP. – OddEssay Apr 13 '15 at 11:02

3 Answers3

1

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?

Anto S
  • 2,448
  • 6
  • 32
  • 50
  • It is like so, and yes the row is dynamic too. – MrAxlee Apr 13 '15 at 11:11
  • @MrAxlee Is it exactly like above? Check updated one? – Anto S Apr 13 '15 at 11:13
  • http://i.imgur.com/xP8HeVp.png (most of these fields are unrelated) Parameters up the top, products down the left. – MrAxlee Apr 13 '15 at 11:16
  • @MrAxlee you mean for each product you can add many parameter? – Anto S Apr 13 '15 at 11:21
  • Yes - in my table in the original post the products are the "test", "test2" etc, and all the checkboxes and text fields in their row are parameters for that product only – MrAxlee Apr 13 '15 at 11:31
  • @MrAxlee So these parameters can not be added in listing the products right? While adding a product you can add many parameters? – Anto S Apr 13 '15 at 11:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75102/discussion-between-mraxlee-and-imaphpdeveloper). – MrAxlee Apr 13 '15 at 11:48
0

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.

MrAxlee
  • 155
  • 1
  • 10
0

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>';
}
Community
  • 1
  • 1