Stay testing until a variable is created in php

-4

I am sending an ajax to a table, but this table can only be created after receiving the value, however it is being created before in blank, how can I make a check for the table to be generated only after receiving the ajax value , stay in a loop by testing the variable to be interrupt the php code until the variable receives its correct value.

I tried this and it was not:

 if (isset($_POST['IdAparelho']))
            {
              $query = ("select id_principal from componentes where id_secundario = ".$_POST['IdAparelho']." ");
              $ids = mysqli_query($con,$query);
              while ($linha = mysqli_fetch_array($ids))
              {
                  $con = mysqli_connect("localhost","roberto","","manutencao");
                  $query = ("select * from componentes where id_principal = ".$linha[0]." ");
                  $componentes = mysqli_query($con,$query);
                  $resultado = mysqli_fetch_row($componentes);

                  $id_componente = $resultado[1];
                  $codigo = $resultado[2];
                  $nome = $resultado[3];
                  $entrada = $resultado[4];
                  $saida = $resultado[5];
                  $f = $resultado[6];
                  $m = $resultado[7];
                  $g = $resultado[8];
                  $gg = $resultado[9];
                  $total = ($f + $m + $g + $gg);

                  $id_componente = $resultado[1];
                  $codigo = $resultado[2];
                  $nome = $resultado[3];
                  $entrada = $resultado[4];
                  $saida = $resultado[5];
                  $f = $resultado[6];
                  $m = $resultado[7];
                  $g = $resultado[8];
                  $gg = $resultado[9];
                  $total = ($f + $m + $g + $gg);

                  print "<tr>";
                    print "<td><strong>".$id_componente."</strong></td>";
                    print "<td><strong>".$codigo."</strong></td>";
                    print "<td><strong>".$nome."</strong></td>";
                    print "<td><strong>".$entrada."</strong></td>";
                    print "<td><strong>".$saida."</strong></td>";
                    print "<td><strong>".$f."</strong></td>";
                    print "<td><strong>".$m."</strong></td>";
                    print "<td><strong>".$g."</strong></td>";
                    print "<td><strong>".$gg."</strong></td>";
                    print "<td><strong>".$total."</strong></td>";
                  print "</tr>";
                }
            }
          ?>

AJAX CODE:

     $(function () {    
        $("#visualizarAparelhos").submit(function (e) {
        var index = $("#IdAparelho").attr('data-index');
        e.preventDefault();
        $.ajax({
          type: "POST", 
          url: "administrador.php",
          dataType: 'html',
          data: {IdAparelho: index}
    }).done(function (data) {
        console.log(data);
    }); 
    });
    });
    
asked by anonymous 06.12.2016 / 12:45

1 answer

0

1 ° - you open a connection for each query line "select id_principal from componentes where id_secundario = ".$_POST['IdAparelho']." I suggest you open 1 connection only and close it after the loop

2nd - this situation could not be resolved with

if (isset($_POST['IdAparelho']))
        {
          $query = ("select * from componentes where id_secundario = ".$_POST['IdAparelho']." ");
          $ids = mysqli_query($con,$query);
          while ($linha = mysqli_fetch_array($ids))
          {

              $id_componente = $linha[1];
              $codigo        = $linha[2];
              $nome          = $linha[3];
              $entrada       = $linha[4];
              $saida         = $linha[5];
              $f             = $linha[6];
              $m             = $linha[7];
              $g             = $linha[8];
              $gg            = $linha[9];
              $total         = ($f + $m + $g + $gg);

3rd - Where do you "test the variable that will break the php code"?

    
06.12.2016 / 13:53