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

0

Well, I'm going to try to be specific here, I need to display all the categories and subcategories of levels 1, 2 and 3 that exist in the bank.

I have achieved this so far, but when he does not find anything on the referring bench, he breaks the html. Could someone give me an aid?

Mycodesofarislikethis.

<tr><!--CHAMANDOCATEGORIA--><?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; ?>
    
asked by anonymous 26.09.2018 / 23:22

1 answer

0

As you do 3 different queries, you also have different amounts of results, and as each cell in your table is a result, you end up with each row in your table with different cell numbers.

Ideally you think of a query that already brings the data the way you want, including with the empty categories, in addition to making your life simpler, will make your page faster (since you will not have 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; ?>
    
27.09.2018 / 00:24