Hi. I took courses to learn PHP and MySql. Even with all I learned I find it hard to put it in practice.
On my new script I got stuck on category and subcategory. I have an working code but I have big doubts about it. I don't think this is the right and pro way to do it. Is just feels like an amateur way to go about it.
Can someone tell me the right way to do it?
Table categories has all categories and subcategories with id, cat_name, position, parent.
My code.
$cat_result = $db->query("SELECT * FROM categories WHERE parent = 0 ORDER BY position ASC");
while ($cat = $db->fetch_array($cat_result)) {
$sub_cid = $cat['id'];
echo "<li><a href=\"\">" . $cat['cat_name'] . "</a>";
$subcat_result = $db->query("SELECT * FROM categories WHERE parent = '".$sub_cid."' ORDER BY position ASC");
while ($cat = $db->fetch_array($subcat_result)) {
echo "<ul>";
echo "<li><a href=\"\">" . $cat['cat_name'] . "</a></li>";
echo "</ul>";
}
echo "</li>";
}