Html/php tables do not appear

Asked

Viewed 102 times

-3

I have this code, but the goal was when you click if you see, the table appears, the problem is that every time I click everything disappears, and does not create the table. If I remove the form it gives, but I have to present the 2 tables, and I create that presented according to the user’s choice.

<!--Menu ver-->
      <div id="VFunc">
      <form action = "" method = "post">
        <b>Qual pretenda ver:</b><br>
        <input type="radio" name="gender" value="Dep" required> Departamento
        <input type="radio" name="gender" value="Set" required> Setor
        <input type="submit" name="BtnP" value="Ver">
      </form>
      <?php 
      if (isset($_POST["BtnP"])){
        if ($_POST["gender"]=="Dep"){
          echo "<table>";
          echo"<tr>";
          echo"<th>Id Departamento</th>";
          echo"<th>Nome Departamento</th>";
          echo"</tr>";
          $sql="SELECT * FROM departamentos";
          $result = $conn->query($sql);
        if ($result->num_rows > 0) {
              while($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>$row[IdDepartamento]</td>";
                echo "<td>$row[NomeDepartamento]</td>";
                echo"</tr>";
                echo"</table>";
              }
          }else{
            echo "0 resultados";
          }
        }elseif ($_POST["gender"]=="Set"){ 
          echo "<table>";
          echo"<tr>";
          echo"<th>Id Setor</th>";
          echo"<th>Nome Setor</th>";
          echo"</tr>";
          $sql="SELECT * FROM setores"; 
          $result = $conn->query($sql);
          if ($result->num_rows > 0) {
          while($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>$row[IdSetor]</td>";
            echo "<td>$row[Nome]</td>";
            echo"</tr>";
            echo"</table>";
          }
            } else {
              echo "0 resultados";
          } 
        }
      }
          $conn->close();
            ?>
      </div>

2 answers

0

In your code you can notice that you are creating the tables dynamically incorrectly.

If the condition is true for $_POST["gender"]=="Dep" or for the $_POST["gender"]=="Set" your code will have the following behavior.

The tag <table> will be created when the if is true and will only be closed (</table>) when there are records in the table. If there are no records in the table, the tag will not be closed and will execute the echo "0 resultados";. But also, if there are records, the closure is within the loop while, i.e., for each record the table will be closed (</table>).

Solution:

<?php
if (isset($_POST["BtnP"])) {
    if ($_POST["gender"] == "Dep") {
        $sql = "SELECT * FROM departamentos";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            echo "<table>";
            echo "<tr>";
            echo "<th>Id Departamento</th>";
            echo "<th>Nome Departamento</th>";
            echo "</tr>";
            while ($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>$row[IdDepartamento]</td>";
                echo "<td>$row[NomeDepartamento]</td>";
                echo "</tr>";
            }
            echo "</table>";
        } else {
            echo "0 resultados";
        }
    }

    if ($_POST["gender"] == "Set") {
        $sql = "SELECT * FROM setores";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            echo "<table>";
            echo "<tr>";
            echo "<th>Id Setor</th>";
            echo "<th>Nome Setor</th>";
            echo "</tr>";
            while ($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>$row[IdSetor]</td>";
                echo "<td>$row[Nome]</td>";
                echo "</tr>";

            }
            echo "</table>";
        } else {
            echo "0 resultados";
        }
    }
}
$conn->close();

-1

<div id="VFunc">
          <form action = "" method = "post">
            <b>Qual pretenda ver:</b><br>
            <input type="radio" name="gender" value="Dep" required> Departamento
            <input type="radio" name="gender" value="Set" required> Setor
            <input type="submit" name="BtnP" value="Ver">
          </form>
          <?php 
          if (isset($_POST["BtnP"])){
            if ($_POST["gender"]=="Dep"){
              echo "<table>";
              echo"<tr>";
              echo"<th>Id Departamento</th>";
              echo"<th>Nome Departamento</th>";
              echo"</tr>";
              $sql="SELECT * FROM departamentos";
              $result = $conn->query($sql);
            if ($result->num_rows > 0) {
                  while($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>$row[IdDepartamento]</td>";
                    echo "<td>$row[NomeDepartamento]</td>";
                    echo"</tr>";
                    echo"</table>";
                  }
              }else{
                echo "0 resultados";
              }
            }elseif ($_POST["gender"]=="Set"){ 
              echo "<table>";
              echo"<tr>";
              echo"<th>Id Setor</th>";
              echo"<th>Nome Setor</th>";
              echo"</tr>";
              $sql="SELECT * FROM setores"; 
              $result = $conn->query($sql);
              if ($result->num_rows > 0) {
              while($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>$row[IdSetor]</td>";
                echo "<td>$row[Nome]</td>";
                echo"</tr>";
                echo"</table>";
              }
                } else {
                  echo "0 resultados";
              } 
            }
          }
              $conn->close();
                ?>
          </div>

Browser other questions tagged

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