I hope to create a recursive function that has no idea yet
this is my code to retrieve a category from a database
<?php
$sql = mysql_query("SELECT * FROM categories WHERE category_parent = '1' ORDER BY lft ASC");
while($row = mysql_fetch_array($sql)) {
echo "<li><a href='/{$row['category_safe_name']}/'>{$row['category_name']}</a>";
$sql2 = mysql_query("SELECT * FROM categories WHERE category_parent = '{$row['category_id']}'");
if(mysql_num_rows($sql2) > 0)
echo "<ul>";
while($row2 = mysql_fetch_array($sql2)) {
echo "<li><a href='/{$row2['category_safe_name']}/'>{$row2['category_name']}</a><li>";
}
if(mysql_num_rows($sql2) > 0)
echo "</ul>";
echo "</li>";
}
?>
Currently it looks like
Top Category (category_id = 1)
Category
Sub Category
My code works for categories and subcategories. I plan to have my code support unlimited subcategories
Any help and recommendations are appreciated.
thank
source
share