0

I have an array of checkboxes.

<input type="checkbox" name="selection[]"  value="move" /> 
<input type="checkbox" name="selection[]"  value="move2" /> 
<input type="checkbox" name="selection[]"  value="move3" /> 
<input type="checkbox" name="selection[]"  value="move4" /> 

Depending on the number of checkboxes selected, a table with corresponding number of rows is generated.

for($x=0; $x<$N; $x++)
{
    echo nl2br("<td><textarea name=art[] rows=10 cols=30></textarea>   </td><td><textarea name=science[] rows=10 cols=30></textarea></td></textarea></td><td><textarea name=method[] rows=10 cols=30></textarea></td><td><textarea name=criteria[] rows=10 cols=30></textarea></td></tr>"); 
}

I cannot tell how many table rows with corresponding columns will be generated each time. So how to write the code to insert each set of row array is a problem. I have tried the

$optionsVal = implode(",", $data);

but that only works to store the selected options and not for the generated table rows and columns.Please can anyone help with this. Thanks in advance

4
  • I don't get it. Can you try to explain your problem with other words? Commented Feb 12, 2013 at 13:59
  • Implode is used to join array elements with a string...how is that relevant to your question of storage of multiple rows and columns in an array...that's in fact, what an array is... Commented Feb 12, 2013 at 14:02
  • Normalize the database Commented Feb 12, 2013 at 14:10
  • @hek2mgl for every selected checkbox, a table row is generated. i want to store the the content in the generated rows and columns in the db. i want to know how write the code and query Commented Feb 12, 2013 at 15:48

1 Answer 1

1

Okay so I think I understand a little better, but perhaps you should relay your question in other terms.

Basically my understanding is that you are accepting an uncertain (within the boundaries of the number of checkboxes you have) number of checkboxes, which there in turn generate a row for each selected check box.

If you want to store these generated rows in mySQL you need to post the data back to the database

$result = mysqli_query($query, $conn);
$row = mysqli_fetch_array($result);

You need to set a $result similar to this, and store your check box values in it In this example if the end-user hits the save button it inserts the values from the check box into a variable

if(isset($_POST["savebtn"]))
{
    //inserting the new information
    $id = $_POST[""];
    $name = $_POST[""]; 



    //iterate through each checkbox selected

foreach($_POST["checkbox"] as $loc_id)
{
  $query = "INSERT INTO table(ID, Loc_Code) VALUES('$id', '$loc_id')";
  $result = mysqli_query($query, $conn);
}


?>

This was just kinda taken from another example, but you are way off with the implode, you need to save the results of the php selection to variables first, and then assign them rows in mySQL by looping through the selection

UPDATE:

Okay, so you got them in an array, seelction[] - this is good now you would want to check to see if a certain value is selected...

if (in_array("move2", $_POST['selection'])) { /* move2 was selected */}

then you want to put that into a single string - you were right with the implode method

echo implode("\n", $_POST['selection']);

then echo it out with a foreach loop

foreach ($_POST['selection'] as $selection) {
     echo "You selected: $selection <br>";
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, the checkboxes are inserting into a table (options). but this does not insert the content of the generated rows. pls could you clarify some more? t
it gives me an error of "column count does not match value count at row 1 " when i try to use this method to insert the generated table
start small - i updated answer to steps - you want to make sure your sql insert statement matches the column count with appropriate values otherwise it will bomb out with the column count does not match error
that's a sql error and you can google the error, it is rather simple to fix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.