How to bring information from the database and insert into a table (bootstrap)

1

Good afternoon! I have tried to bring information from the database 'user' and insert it into a table that follows the bootstrap syntax. But because of the complexity of tags I end up wrapping myself around: (

  

The goal is: > Select a teacher 'user'. And when it is selected, bring the disciplines'usuario_disciplina 'which it has already registered to the banks.

<form class="form-horizontal" method="POST"  action="php/CadastrarDisciplina.php" style="margin-top:20px;">
<fieldset>
  <!-- Form Name -->
  <!-- Text input-->
  <div class="form-group">
    <label class="col-md-3 control-label" for="nome">Professor:</label>
      <div class="col-md-4">
        <select id="idusuario" name="idusuario" class="form-control">
          <option> Selecione o professor...</option>
          <?php
          //Seleciona todos os professores e lista no form
          include 'php/Conexao.php';
          $stmt = $conexao->prepare("SELECT * FROM usuario ");
          $stmt->execute();

         if($stmt->rowCount()>0){
           $resultado = $stmt->fetchAll();
           foreach($resultado as $linha){ 
         ?>
           <option value="<?php echo $linha['idusuario']; ?>"><?php echo ($linha['nomeusuario']); ?></option>
        <?php
           }
        }
        ?>
        </select>
      </div>
  </div>
</fieldset>

<!-- Quadro com todas as disciplinas-->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel='stylesheet' type='text/css'>

<div class="container">
 <div class="row">
  <p></p>  
  <div class="col-md-7 col-md-offset-1">
   <div class="panel panel-default panel-table">
    <div class="panel-heading">
     <div class="row">
      <div class="col col-xs-6">
       <h3 class="panel-title">Lista de Disciplinas</h3>
      </div>
      <div class="col col-xs-6 text-right">
      </div>
     </div>
    </div>
    <?php                      
     include 'php/Conexao.php';
     $stmt = $conexao->prepare("select * from disciplina");
     $stmt->execute();
     if($stmt->rowCount() >0){
    ?>
    <div class="panel-body">
     <table class="table table-striped table-bordered table-list">
      <thead>
       <tr>
        <th><em class="fa fa-cog"></em></th>
        <th class="hidden-xs">ID</th>
        <th>Nome</th>
       </tr> 
      </thead>
      <?php 
        } 
        $resultado = $stmt->fetchAll();

        foreach($resultado as $linha){
          $stmt2 = $conexao->prepare("SELECT * FROM usuario_disciplina WHERE usuario_idusuario=? and disciplina_iddisciplina=?;");
          $stmt2 -> bindParam(1,$idusuario);
          $stmt2 -> bindParam(2,$linha['iddisciplina']);
          $stmt2->execute();
          $resultado2 = $stmt2->fetchAll();
      ?>
      <tbody>
       <tr>
         <td align="center">
          <input type="checkbox" class="form-check-input"  <?php if($stmt2->rowCount()>0){echo "checked='checked'"; }?>  value=<?php echo $linha["iddisciplina"]; ?>>
         </td>
         <td class="hidden-xs"><?php echo $linha["iddisciplina"]; ?></td>
         <td><?php echo ($linha["descricaodisciplina"]); ?></td>
       </tr>
      </tbody>
      <?php 
        } 
        ?>
      </table>
    </div>
   </div>
  </div>
  <button type="submit"  method="POST" name="singlebutton"  style="margin-left:35%;"    class="btn btn-success">Salvar disciplinas</button>   
 </div>
</div>
</form>
    
asked by anonymous 28.12.2017 / 22:17

1 answer

2

Your loop of repetition with information coming from the database needs to encompass each row () to add the information in columns ()

<tbody>
<?php 
    foreach($resultado as $linha){
        $stmt2 = $conexao->prepare("SELECT * FROM usuario_disciplina WHERE usuario_idusuario=? and disciplina_iddisciplina=?;");
        $stmt2 -> bindParam(1,$idusuario);
        $stmt2 -> bindParam(2,$linha['iddisciplina']);
        $stmt2->execute();
        $resultado2 = $stmt2->fetchAll();
?>
        <tr>
            <td align="center">
                <input type="checkbox" class="form-check-input"  <?php if($stmt2->rowCount()>0){echo "checked='checked'"; }?>  value=<?php echo $linha["iddisciplina"]; ?>>
            </td>
            <td class="hidden-xs"><?php echo $linha["iddisciplina"]; ?></td>
            <td><?php echo ($linha["descricaodisciplina"]); ?></td>
        </tr>
<?php 
    } 
?>
</tbody>
    
29.12.2017 / 12:28