Take data from a dynamic HTML table

Asked

Viewed 439 times

0

I have a dynamic table in HTML which brings the following result:

inserir a descrição da imagem aqui

The columns Português and Mathematics, comes from the database and are dynamic, ie may contain more materials. The same occurs with the column Students, which is also dynamic and comes from the database.

Note that Fernando Pessoa’s notes are: Portuguese 11 and Mathematics 12 and Santos Dumont’s is Portuguese 13 and Mathematics 14.

To generate this table, I did it as follows. (correct me if I’m done wrong):

public function materiasNotas($idEscolas,$idTurmas){
.....
$listar = "<table class=\"table table-bordered\">
                  <thead>
                    <tr>
                   <th style='background-color: #4682B4; color: #FFF; text-align: center'>Alunos</th>";
       while($jmListar = mysqli_fetch_object($sqlListar)){  
             $listar .= "<th style='background-color: #4682B4; color: #FFF; text-align: center'>".$jmListar->Materias."</th>";
        }    
             $listar .= "<th style='background-color: #4682B4; color: #FFF; text-align: center'>Boletim</th>
                    </tr>
                  </thead>";

       $sqlAlunos = mysqli_query($this->conexao,"SELECT * FROM pe_cadastros_alunos WHERE IdEscolas = '".$idEscolas."' AND IdTurmas = '".$idTurmas."';");

       while($jmAlunos = mysqli_fetch_object($sqlAlunos)){
             $listar .= "<tbody>";
             $listar .= "<td style='background-color: #B0C4DE'><input type='text' name='Alunos[]' value='".$jmAlunos->NomeCompleto."' style='border: 0; background-color: #B0C4DE;' readonly></td>";
             //$i = 1;
             $sqlMat = mysqli_query($this->conexao,"SELECT * FROM pe_materias WHERE IdEscolas = '".$idEscolas."' AND IdSeries = '".$jmTurmas->Series."';");
             while($jmMat = mysqli_fetch_object($sqlMat)){                   
                 $listar .= "<td><div align='center'><input type='hidden' name='Materias[]' value='".$jmMat->Materias."'><input type='text' name='Notas[]' placeholder='Nota' style='width: 100px'></div></td>";                
             } 
             $listar .= "<td><div align='center'><button class='btn btn-success btn-xs'><i class=\"fa fa-print fa-lg\" aria-hidden=\"true\"></i> Imprimir</button> <button class='btn btn-success btn-xs'><i class=\"fa fa-envelope fa-lg\" aria-hidden=\"true\"></i> E-mail</button> <button class='btn btn-success btn-xs'><i class=\"fa fa-download fa-lg\" aria-hidden=\"true\"></i> Baixar</button></div></td>";
             $listar .= "</tbody>";
        }  
         $listar .= "</table>";
         return $listar;    
}

The problem is time to register in the database, because it is returning me this way:

Note that the names are not matching with the notes cited above, IE, in the second line was to be Fernando Pessoa and Santos Dumont was to stay in the third and fourth lines.

inserir a descrição da imagem aqui

I’m registering that way:

public function cadastrarNotasAlunos($idEscola,$aluno,$bimestre,$materias,$notas,$situacao){
 ....    
    for($i = 0; $i <= count($notas) - 1; $i++){
            mysqli_query($this->conexao, "INSERT INTO pe_notas_alunos VALUES(null,'".$idEscola."','".$aluno[$i]."','".$materias[$i]."','".$notas[$i]."','".   $bimestre."','".$situacao."');");
    }    
} 

How I would have the names registered according to your notes?

1 answer

1


Friend I believe that in its function of registering the student is not a matrix but a constant. No increment required in case more than one note comes.

mysqli_query($this->conexao, "INSERT INTO pe_notas_alunos VALUES(null,'".$idEscola."','".$aluno."','".$materias[$i]."','".$notas[$i]."','".   $bimestre."','".$situacao."');");

php is not my forte, the ideal solution there would be the creation of the object pe_notas_alunos and would be sent an array of pe_notas_alunos.

however I believe that the code below should work

for($a = 0; $a <= count($aluno) - 1; $a++){  
    for($i = a*(count($notas)/count($aluno)) $i <= (count($notas)/count($aluno)) + a*(count($notas)/count($aluno)) - 1; $i++){
          mysqli_query($this->conexao, "INSERT INTO pe_notas_alunos VALUES(null,'".$idEscola."','".$aluno[$a]."','".$materias[$i]."','".$notas[$i]."','".   $bimestre."','".$situacao."');");
    }
}
  • Hello Felipe. Actually the student is a matrix name='Students[]'

  • The problem is exactly there when you pass the student matrix with 2 elements and the note matrix with 4 elements and iterate with the same variable.

  • Got it.... could you give me an example? Because I really don’t know what to do...

  • Hello Felipe. I will try with your code...

Browser other questions tagged

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