I'm building a system of infinite, simple categories and subcategories. I found a script on the internet that meets the requirements, but its customization and adaptation to my project made it almost unusable.
I studied a simple structure and adapted the database and the registration script correctly, but my biggest problem is being in the display part.
Based on the following records:
ListingthewayIhaveadapted(incorrectrecital)Ihavesomelimitationsbecausewhenitcomestochildrenofsubcategoriesalreadychildrenarenotlisted,Ineedasimplescriptthatdoessuchprocedureandiseasytocustomize.
Icurrentlyhavethefollowingresult:
My script for the current listing:
<?php
echo "<ul>";
$selCategorias2 = $conn->prepare("SELECT * FROM cat WHERE cat_pai = ?");
$selCategorias2->execute(array(0));
while($rowCategoria2 = $selCategorias2->fetch(PDO::FETCH_OBJ)){
echo $rowCategoria2->cat_nome."<BR/>";
$selFilhos = $conn->prepare("SELECT * FROM cat WHERE cat_pai = ?");
$selFilhos->execute(array($rowCategoria2->cat_id));
while($rowFilho = $selFilhos->fetch(PDO::FETCH_OBJ)):
echo " ".$rowFilho->cat_nome."<br/>";
$selSubFilhos = $conn->prepare("SELECT * FROM cat WHERE cat_pai = ?");
$selSubFilhos->execute(array($rowFilho->cat_id));
while($rowSubFilho = $selSubFilhos->fetch(PDO::FETCH_OBJ)):
echo " ".$rowSubFilho->cat_nome."<br/>";
endwhile;
endwhile;
} //fecha while categoria raiz
echo "</ul>"; ?>
Note: I know there are other ways to space the category other than "& nbsp" but in this case it's just for testing. Home The script I mentioned earlier uses FETCH_ASSOC instead of FETCH_OBJ