[eluser]woomacoder[/eluser]
[quote author="Dam1an" date="1240616391"]I still don't see the point of storing the number of sub categories in the cats table, as you risk getting out of sync, and its very quick to do a count on the number of records where parent_id == cat_id
Could you not do something like
Code:
<?php
$categories = get_categories();
foreach($categories as $category) {
// Print the top level category
echo $category->name, '<br />';
// get the sub categories
$subcategories = get_categories($category->id);
foreach($subcategories as $subcategory) {
echo '- ', $subcategory->name, '<br />';
}
}
function get_categories($parent_id = 0) {
$this->db->where('parent_id', $parent_id);
return $this->db->get('categories')->result();
}
?>
[/quote]
Yeah!
Great solution!
thank you!