I have the following form.
<!DOCTYPE html>
<html>
<head><title>ARREGLO_BID</title></head>
<body>
<form method="POST" action="pruebados.php">
<select name="form[][txtmateria]">
<option selected value="">SELECCIONE MATERIA</option>
<option value="ADMINISTRACION">ADMINISTRACION</option>
<option value="COMERCIO">COMERCIO</option>
<option value="SISTEMAS">SISTEMAS</option>
</select>
<label><input name="form[][txtgrado]" value="SALA_1" type="checkbox">SALA_1</label>
<label><input name="form[][txtgrado]" value="SALA_2" type="checkbox">SALA_2</label>
<label><input name="form[][txtgrado]" value="SALA_3" type="checkbox">SALA_3</label>
<input type="submit" name="btnenviar" value="ENVIAR">
</form>
</body>
</html>
if ( isset( $_POST['form'] ) ){
echo '<table border="1">';
echo '<thead>';
echo '<tr>';
echo '<th>MATERIA</th>';
echo '<th>GRADO</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ( $_POST['form'] as $diam ) {
echo '<tr>';
echo '<td>', $diam['txtmateria'],'</td>';
echo '<td>', $diam['txtgrado'], '</td>';
echo '</tr>'; }
echo '</tbody>';
echo '</table>';}
The idea is that, for example, when selecting a subject, this will repeat the number of times of Checkbox selections in the table, if you select a subject and two Checkbox, it will appear something like this:
But I have the following errors.
According to the suggestions of Kleith , modifying the name attributes like this:
<select name="form[txtmateria]">
<input name="form[txtgrado][]" type="checkbox">
And reordering in foreach for the file tested.php and implementing a for cycle to print the array inside the foreach, like this:
if ( isset( $_POST['form'] ) ){
echo '<table border="1">';
echo '<thead>';
echo '<tr>';
echo '<th>MATERIA</th>';
echo '<th>GRADO</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ( $_POST['form'] as $txtgrado ) {
for($x=0;$x<count($txtgrado);$x++){
echo '<tr>';
echo '<td>' .$_POST['form']['txtmateria'].'</td>';
echo '<td>' .$txtgrado[$x].'</td>';
echo '</tr>'; }
}
echo '</tbody>';
echo '</table>'; }
I get a small detail on the first line when printing (a letter "A" that I do not know where it comes from), following the example I proposed to select a subject and two rooms, which would affect me when entering a BD, like this: