I need to return an empty td if there is data in the database

Asked

Viewed 38 times

0

Guys, I’m going to try to be specific here, I need to show all the categories and subcategories of level 1, 2 and 3 that exist in the bank.

I got it so far, but when he can’t find anything in the referring bank, he breaks the html. Could someone give me a hand?

inserir a descrição da imagem aqui

My code so far is like this.

<tr>


    <!--CHAMANDO CATEGORIA-->
    <?php
    $select_cat = "SELECT * FROM categoria_recursiva WHERE nivel = 0";
    $res_selectCat = $conn->query($select_cat);
    while($colCat = mysqli_fetch_assoc($res_selectCat)):
        $idCat = $colCat['id'];
    ?>
    <td> <?= $colCat['nome']; ?> </td>

    <!--CHAMANDO A SUBCATEGORIA 1-->
    <?php
    $select_subcat1 = "SELECT * FROM categoria_recursiva WHERE id_pai = $idCat";
    $res_selectSub1 = $conn->query($select_subcat1);
    while($colSub1 = mysqli_fetch_assoc($res_selectSub1)):
            $idSub1 = $colSub1['id'];
    ?>
        <td> <?= $colSub1['nome']; ?> </td>

    <!--CHAMANDO A SUBCATEGORIA 2-->
    <?php
        $select_sub2 = "SELECT * FROM categoria_recursiva WHERE id_pai = '$idSub1'";
        $res_selectSub2 = $conn->query($select_sub2);
        while($colSub2 = mysqli_fetch_assoc($res_selectSub2)):
            $idSub2 = $colSub2['id'];
        ?>
        <td> <?= $colSub2['nome'];?> </td>

            <!--CHAMANDO A SUBCATEGORIA 3-->
            <?php
            $select_sub3 = "SELECT * FROM categoria_recursiva WHERE id_pai = '$idSub2'";
            $res_selectSub3 = $conn->query($select_sub3);
            while($colSub3 = mysqli_fetch_assoc($res_selectSub3)):
                $idSub3 = $colSub3['id'];
                ?>
                <td> <?= $colSub3['nome'];?> </td>

            <?php endwhile; ?>

        <?php endwhile; ?>





    <td>
        <button class="btn btn-info" type="submit" data-toggle="modal" data-target="#painel-editar<?= $colCat['id']; ?>" data-placement="top" title="" data-original-title="Editar"><i class="fa fa-edit"></i> </button>
        <button class="btn btn-danger" type="submit" data-toggle="modal" data-target="#painel-remover<?= $col_cat['id']; ?>" data-placement="top" title="" data-original-title="Apagar"><i class="fa fa-close"></i> </button>
    </td>
</tr><?php endwhile; ?>
  • Enter the code, not an image of it. The site has support for this.

  • Apparently the number of <td> is different for each <tr> on your table.

  • I just edited by going up the code

1 answer

0

As you make 3 different queries, you also have different amount of results, and as each cell of your table is a result, you end up with each row of your table with different number of cells.

The ideal is that you think about a query that already brings the data the way you want, including with the empty categories, besides making your life simpler, will make your page faster (since you will not need to go in the bank 3 times), I did what I imagine would be the select you need, if it is not, you can make the adjustments, the goal is just to give the idea of what to do:

SELECT cp.id, cp.nome as catpai, sc1.nome as subcat1, sc2.nome as subcat2, sc3.nome as subcat3
FROM categoria_recursiva cp
LEFT JOIN categoria_recursiva sc1 ON sc1.id_pai = cp.id
LEFT JOIN categoria_recursiva sc2 ON sc2.id_pai = sc1.id
LEFT JOIN categoria_recursiva sc3 ON sc3.id_pai = sc2.id
WHERE cp.id_pai is null -- para saber que cp vai ser apenas da categoria pai

From here you need only a while to create the table:

<?php
query($select_cat); // $select_cat é o sql acima
while($colCat = mysqli_fetch_assoc($res_selectCat)):
?>
<tr>
    <td> <?= $colCat['catpai']; ?> </td>
    <td> <?= $colCat['subcat1']; ?> </td>
    <td> <?= $colCat['subcat2']; ?> </td>
    <td> <?= $colCat['subcat3']; ?> </td>
    <td>
        <button class="btn btn-info" type="submit" data-toggle="modal" data-target="#painel-editar<?= $colCat['id']; ?>" data-placement="top" title="" data-original-title="Editar"><i class="fa fa-edit"></i> </button>
        <button class="btn btn-danger" type="submit" data-toggle="modal" data-target="#painel-remover<?= $colCat['id']; ?>" data-placement="top" title="" data-original-title="Apagar"><i class="fa fa-close"></i> </button>
    </td>
</tr>
<?php endwhile; ?>

Browser other questions tagged

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