CodeIgniter Forums
Help with this query .... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Help with this query .... (/showthread.php?tid=2002)



Help with this query .... - El Forum - 07-11-2007

[eluser]Unknown[/eluser]
Hi every:
I'm developing a web application and now I'm in a crash point. I have two tables. Basically they have this fields:
Code:
smp_catgaleria
------------------------
|idc INT(11) PK NN AUTOINCREMENT
|title NN VARCHAR(35)
|...
------------------------

smp_subcatgaleria
------------------------
|idsc PK NN AUTOINCREMENT
|idc INT(11) NN
|title NN VARCHAR(35)
|...
------------------------
I try to build a simple menu from this data but I need to get every subcategory when I have the category. For example:
Code:
$idc = $this->gal_model->getCategories();
foreach($idc as $cat)
{
$this->gal_model->getSubCategoriesFromCategory($cat->idc);
}
I made a simple helper for build a menu system. This menu have main menu and submenus. How you develop this?


Help with this query .... - El Forum - 07-11-2007

[eluser]Rick Jolly[/eluser]
As long as every top level category has only one level of subcategories, your example code is almost there. Maybe create a menu array that looks something like this:

Code:
$array_menu = array();
$array_categories = $this->gal_model->getCategories();
foreach($array_categories as $category)
{
    $array_subcategories = $this->gal_model->getSubCategoriesFromCategory($category->idc);

    // add this category and its subcategories to the menu array
    $array_menu[] = array($category,$array_subcategories);
}

If you need multiple levels of subcategories you'll need to use recursion. Check out examples of "Adjacency List". You could also use "Nested Sets" which are more complicated. There are CI examples of both methods out there.


Help with this query .... - El Forum - 07-11-2007

[eluser]Unknown[/eluser]
Thanks Rick I check and come back later if I have any problem.
Cheers