Table with 2 td with select inputs displaying return only on the first line

2

I have 1 table with 1 list of modules, and I have 2 columns that have some options that can be chosen through select.  

The issue is that the select's only loads the data in the first row of the table, the next comes out blank, I think that's the way I'm using my loops that may be wrong.

Here is the code for my table:

    <?php
    echo" <div class='table-responsive'>
    <table class='table lista-clientes table-striped table-hover table-bordered table-condensed'>
      <thead>
        <tr>
          <th>SISTEMA</th>
          <th>STATUS</th>
          <th>NIVEL</th>
          <th align='center'>
              SALVAR
          </th>
        </tr>
      </thead>
      <tbody>";
        while($rowModulosSistemas = mysqli_fetch_assoc($resultModulosSistemas)) {
          echo"
          <tr>
            <td>".$rowModulosSistemas["mod_sist_titulo"]."</td>
            <td>
              <select class='form-control' id='status' name='status'>
                <option></option>";
                while($rowStatus = mysqli_fetch_assoc($resultStatus)) {
                  echo"<option value='".$rowStatus["STATUS"]."'>".$rowStatus["DESCRICAO"]."</option>";
                }
                echo"
              </select>
            </td>
            <td >
              <select class='form-control' id='nivel' name='nivel'>
                <option></option>";
                while($rowNivelUsuarios = mysqli_fetch_assoc($resultNivelUsuarios)) {
                  echo"<option value='".$rowNivelUsuarios["ID"]."'>".$rowNivelUsuarios["DESCRICAO"]."</option>";
                }
                echo"
              </select>
            </td>
            <td align='center'>
             <button type='submit' class='btn btn-success btn-sm'>
              SALVAR
              <span class='glyphicon glyphicon-ok'>
              </button>
            </td>  
          </tr>";
        }
        echo"
      </tbody>
    </table> 
    ";
    ?>
  

@Luiz MG:

object(mysqli_result)#5 (5) { ["current_field"]=> int(0) ["field_count"]=> int(6) ["lengths"]=> NULL ["num_rows"]=> int(14) ["type"]=> int(0) }

  

Logicfound:

<?php$x=1;$y=1;$z=1;echo"<table>
<thead>
    <tr>
        <th>SISTEMA</th>
        <th>STATUS</th>
        <th>NIVEL</th>
    </tr>
</thead>";
while($x <= 5) {
    echo"
    <tbody>
        <tr>
            <td>
                SISTEMA".$x."
            </td>
            <td>
                <select>";
                    while($y <= 5) {
                        echo"<option>".$y."</option>";
                        $y++;
                    }
                    echo"</select>
                </td>
                <td>
                    <select>";
                        while($z <= 5) {
                            echo"<option>".$z."</option>";
                            $z++;
                        }

                        echo"</select>
                    </td>
                </tr>
            </tbody>";  
            $y = 1;
            $z = 1;
            $x++;
        } 
        echo"</table> ";

This logic found is exactly what I need, how could I implement it in the previous code?

    
asked by anonymous 06.09.2017 / 06:03

1 answer

1

I did not test here, but I believe something like this works.

Whenever you are in doubt as to how the array of a var_dump ($ your_var) is arriving; Since you're using foreach, it should be type like

$rowModulosSistemas[0] = ...;
$rowModulosSistemas[1] = ...;

So I suggest using a for instead of while and make a count before to know how many results are there

$conta_repeticoes = count($resultModulosSistemas);

$rowModulosSistemas = mysqli_fetch_assoc($resultModulosSistemas);
$rowStatus = mysqli_fetch_assoc($resultStatus);
$rowNivelUsuarios = mysqli_fetch_assoc($resultNivelUsuarios);

for($x=0; $x < $conta_repeticoes; $x++) {
    echo"
      <tr>
        <td>".$rowModulosSistemas["mod_sist_titulo"][$x]."</td>
        <td>
          <select class='form-control' id='status' name='status'>
            <option></option>";

              echo"<option value='".$rowStatus["STATUS"][$x]."'>".$rowStatus["DESCRICAO"][$x]."</option>";

            echo"
          </select>
        </td>
        <td >
          <select class='form-control' id='nivel' name='nivel'>
            <option></option>";
            while() {
              echo"<option  value='".$rowNivelUsuarios["ID"][$x]."'>".$rowNivelUsuarios["DESCRICAO"][$x]."
                </option>";
            }
            echo"
          </select>
        </td>

Another tip, that appeals to some people, like me, I find it easier to you read the code, without using the "echo" all the time to print the HTML. Take the test, for example:

instead of:

if($x){
    echo "<div class='qualquer'>A variável $x é verdadeira!</div>";
}

Try:

 <?php 
    if($x){
 ?>
        <div class='qualquer'>A variável <?= $x ?> é verdadeira!</div>     
<?php 
    }
?>

I know it sounds worse at first, but in many cases it uses many html tags with single or double quotes, makes it much easier, you do not keep breaking your head to concatenate everything. Another great advantage is that when you do a large echo like your code, it is easier for you to understand the indentation of the code if you use an IDE or a good text editor.

Hugs!

    
06.09.2017 / 13:37