Table: categories
╔═════════════╦═══════════════════╦═════════════════╗
║ id_category ║ category_name ║ category_father ║
╠═════════════╬═══════════════════╬═════════════════╣
║ 1 ║ Home ║ 0 ║
║ 2 ║ Category_1 ║ 1 ║
║ 3 ║ Category_2 ║ 1 ║
║ 4 ║ Category_3 ║ 1 ║
║ 5 ║ Category_4 ║ 1 ║
║ 6 ║ Category_5 ║ 1 ║
║ 7 ║ Sub_Category_1 ║ 2 ║
║ 8 ║ Sub_Category_2 ║ 2 ║
║ 9 ║ Sub_Category_3 ║ 2 ║
║ 10 ║ Sub_Category_4 ║ 2 ║
║ 11 ║ Sub_Category_5 ║ 3 ║
║ 12 ║ Sub_Category_6 ║ 3 ║
║ 13 ║ Sub_Category_7 ║ 3 ║
║ 14 ║ Sub_Category_8 ║ 3 ║
║ 15 ║ Sub_Category_9 ║ 3 ║
╚═════════════╩═══════════════════╩═════════════════╝
I am using meekro.class to consult my database, right now I'm building the menu of my application, but I need to do JOIN to the same table to show the category_name instead of the id of category_father and I can not.
I also need to show in list form the main categories and below the subcategories. I intend to use only one level of sub categories so this table structure should work.
This is my query:
$querycategories = DB::query("SELECT * FROM categories");
foreach ($querycategories as $row) {
echo "<b>" . $row['category_name'] . "</b>";
echo "<ul>";
echo "<li>" . $row['category_father'] . "</li>";
echo "</ul>";
}
Of course, with this consultation, I get a disaster, that's why I come to you to get help!
This is what I hope to obtain:
Category_1
- Sub_Category_1
- Sub_Category_2
- Sub_Category_3
- Sub_Category_4
Category_2
- Sub_Category_5
- Sub_Category_6
- Sub_Category_7
- Sub_Category_8
- Sub_Category_9
Category_3
Category_4
Category_5
I hope you can help me, it takes almost 1 hour to write the question, please, take 5 minutes of your day to help me, thanks in advance!