PHP - How to navigate a radios array?

Asked

Viewed 192 times

0

I have the following code:

<?php while($row = mysql_fetch_array($alunosTurma)){ ?>
          <tr>
              <td><?php echo $row['NM_NIS_ALU']; ?></td>
              <td><?php echo $row['ST_NOME_ALU']; ?>
                <input type="hidden" value="<?php echo $row['ID_ALUNO_ALU']; ?>" name="aluno[]">
                <!-- <input type="hidden" value="<?php echo $idTurma; ?>" name="turma[]">
                <input type="hidden" value="<?php echo $dtChamada; ?>" name="data[]"> -->
              </td>
              <td class="text-center">
                <div class="btn-group" data-toggle="buttons">
                  <label class="btn btn-default">
                    <input type="radio" name="options[]" value="0"><i class="fa fa-check text-success"></i>
                  </label>
                  <label class="btn btn-default">
                    <input type="radio" name="options[]" value="1"><i class="fa fa-times text-danger"></i>
                  </label>
                </div>
              </td>
          </tr>
          <?php } ?>

This creates a table listing all students and puts in front of each two radios (one for presence and one for lack). In the file that input the database (or should) I have the following:

$data = $_GET['dtChamada'];
$turma = $_GET['idTurma'];
$aluno = $_POST['aluno'];
$status = $_POST['options'];
$cont = 0;
foreach ($aluno as $idAluno) {
mysql_query(" INSERT INTO CHAMADA (DT_CHAMADA_CHMD, ID_TURMA_TUR, ID_ALUNO_ALU, FL_STATUS_CHMD) VALUES ('$data', '$turma', '$idAluno[$cont]', '$status[$cont]') "));
print_r("$data<br>");
print_r("$turma<br>");
print_r("$aluno[$cont]<br>");
print_r("$status[$cont]<br><br>");
$cont++;
}
?>

For the first student arrive the data as accurate, but from the second on the status (presence or lack) gives the following error:

**Notice: Undefined offset: 1 in
C:\xampp\htdocs\NISFRAM\processa\lancaChamada.php on line 26**

How can I scroll through each student recording their status at the bank?

  • Since foreach does not need to index the array, simply $idAluno

1 answer

0

The Table

<?php while($row = mysql_fetch_array($alunosTurma)){ ?>
          <tr>
              <td><?php echo $row['NM_NIS_ALU']; ?></td>
              <td><?php echo $row['ST_NOME_ALU']; ?>
                <input type="hidden" value="<?php echo $row['ID_ALUNO_ALU']; ?>" name="aluno[]">
                <!-- <input type="hidden" value="<?php echo $idTurma; ?>" name="turma[<?php echo $row['ID_ALUNO_ALU']; ?>]">
                <input type="hidden" value="<?php echo $dtChamada; ?>" name="data[<?php echo $row['ID_ALUNO_ALU']; ?>]"> -->
              </td>
              <td class="text-center">
                <div class="btn-group" data-toggle="buttons">
                  <label class="btn btn-default">
                    <input type="radio" name="options[<?php echo $row['ID_ALUNO_ALU']; ?>]" value="0"><i class="fa fa-check text-success"></i>
                  </label>
                  <label class="btn btn-default">
                    <input type="radio" name="options[<?php echo $row['ID_ALUNO_ALU']; ?>]" value="1"><i class="fa fa-times text-danger"></i>
                  </label>
                </div>
              </td>
          </tr>
          <?php } ?>

PHP

<?php
$data = $_GET['dtChamada'];
$turma = $_GET['idTurma'];
$aluno = $_POST['aluno'];
$status = $_POST['options'];

foreach ($aluno as $idAluno) {
mysql_query(" INSERT INTO CHAMADA (DT_CHAMADA_CHMD, ID_TURMA_TUR, ID_ALUNO_ALU, FL_STATUS_CHMD) VALUES ('$data', '$turma', '$idAluno', '$status[$idAluno]') "));
print_r("$data<br>");
print_r("$turma<br>");
print_r("$aluno[$idAluno]<br>");
print_r("$status[$idAluno]<br><br>");
}
?>
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.