Table with 2 td with select inputs showing return only in the first row

Asked

Viewed 87 times

2

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

The point is that the select’s only load the data in the first row of the table, the next comes out blank, I think it’s the way I’m using my loops that might be wrong.

Below is the code of 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) }

inserir a descrição da imagem aqui

Logic found:

<?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?

  • Good night! I am not an expert in php, but analyzing, you are giving 2 loops within a loop, right? Logically, the 2 internal loops will only pull data 1 time. Hence the problem.

1 answer

2


I didn’t test it here, but I believe something like this will work

Whenever you have doubts about how the array of a var_dump($sua_var) is coming; Since you’re using foreach, it must be kind of like this

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

so I suggest using a for instead of while and doing a Count before to know how many results there are

$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 pleases some people, like me, I think it’s easier to you read the code, without using "echo" all the time to print the HTML. Take the test, example:

instead of:

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

try it:

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

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

Hug!

  • Otácio, I think I saw my mistake instead of $rowModulosSistemas["mod_sist_title"][$x], the right is $rowModulosSistemas[$x]["mod_sist_title"], in all cases $x comes before. Test there and tell us, vlw!

  • Give a var_dump($rowModulosSystems); before the is and paste the code here for me to see please. It usually starts from 0, but it may be starting from 1 in your case, then it would have to be for($x=1...), but it passes the var_dump() which is better to confirm.

Browser other questions tagged

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