Welcome Guest, Not a member yet? Register   Sign In
Help with this query ....
#1

[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?
#2

[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.
#3

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




Theme © iAndrew 2016 - Forum software by © MyBB