linx_effect Posted December 4, 2009 Share Posted December 4, 2009 Hi Everyone, I managed to find a function to generate unlimited categories and subcategories which I've modified slightly and currently using it as a navigation with drop down menu. I'm having difficulities to assign a class to the li tag (i.e. <li class="sub"> ) for each category that contains sub categories. In the CSS file, I've set the li.sub:hover to display an arrow. Summary: I need each category that has subcategories to display an arrow. please if anyone can help me with this i'll be very greatful. Here is the code: function generate_nav($parent) { $has_childs = false; global $nav_array; foreach($nav_array as $key => $value) { //$sub = ""; if ($value["page_parent_id"] == $parent) { if ($has_childs === false) { //$sub = "class=\"sub\""; $has_childs = true; echo "<ul>"; } echo "<li $sub><a href='index.php?id=$value[page_id]'>$value[page_title]</a>"; generate_nav($key); echo "</li>"; } } if ($has_childs === true) echo "</ul>"; } Link to comment https://forums.phpfreaks.com/topic/183961-need-help-with-navigation-with-unlimited-categoriessubcategories/ Share on other sites More sharing options...
shedokan Posted December 4, 2009 Share Posted December 4, 2009 Cn you show an example array of what is passed to that function? Link to comment https://forums.phpfreaks.com/topic/183961-need-help-with-navigation-with-unlimited-categoriessubcategories/#findComment-971177 Share on other sites More sharing options...
linx_effect Posted December 4, 2009 Author Share Posted December 4, 2009 Yeah sure, it is: $nav_array[$row['page_id']] = array( 'page_id' => $row['page_id'], 'page_title' => $row['page_title'], 'page_parent_id' => $row['page_parent_id'] ); Then I call the function like this: //pass the page_parent_id to begin the loop from generate_nav(1); Thanks Link to comment https://forums.phpfreaks.com/topic/183961-need-help-with-navigation-with-unlimited-categoriessubcategories/#findComment-971187 Share on other sites More sharing options...
shedokan Posted December 4, 2009 Share Posted December 4, 2009 Try this: function generate_nav($parent){ $has_childs = false; global $nav_array; foreach($nav_array as $key => $value){ if ($value["page_parent_id"] == $parent){ if ($has_childs === false){ $has_childs = true; echo "<ul>"; } $sub =''; foreach($nav_array2 as $key2){ if ($value2["page_parent_id"] == $value['page_id']){ $sub = 'class="sub"'; break; } } echo "<li $sub><a href='index.php?id=$value[page_id]'>$value[page_title]</a>"; generate_nav($key); echo "</li>"; } } if ($has_childs === true) echo "</ul>"; } And instead of you I would use multidiemensional arrays like: $nav_array = array( 1 => array( 'page_title' => 'Page id 1', 'sub_pages' => array( 25 = > array( 'page_title' => 'Page id 25', ) ) ) ); So you can just check if page has sub_pages or not... but that's just me... Link to comment https://forums.phpfreaks.com/topic/183961-need-help-with-navigation-with-unlimited-categoriessubcategories/#findComment-971219 Share on other sites More sharing options...
linx_effect Posted December 4, 2009 Author Share Posted December 4, 2009 Thanks shedokan for the post but unfortunately that didnt work for me, the function with your edit hasn't echoe'd $sub (class="sub") anywhere on the page I think i may need to keep it simple and stick to single dimention array, as i dont have enough experience to play with multidimentional arrays Link to comment https://forums.phpfreaks.com/topic/183961-need-help-with-navigation-with-unlimited-categoriessubcategories/#findComment-971272 Share on other sites More sharing options...
shedokan Posted December 4, 2009 Share Posted December 4, 2009 Oh sorry found an error in my code: function generate_nav($parent){ $has_childs = false; global $nav_array; foreach($nav_array as $key => $value){ if ($value["page_parent_id"] == $parent){ if ($has_childs === false){ $has_childs = true; echo "<ul>"; } $sub =''; // Loop the $nav_array array to check if there's a page which his parent isthis one( $value['page_id'] ) foreach($nav_array as $key2 = >$value2){ if ($value2["page_parent_id"] == $value['page_id']){ $sub = 'class="sub"'; break; } } echo "<li $sub><a href='index.php?id=$value[page_id]'>$value[page_title]</a>"; generate_nav($key); echo "</li>"; } } if ($has_childs === true) echo "</ul>"; } And I added a comment so you can see where I added and what it does. Link to comment https://forums.phpfreaks.com/topic/183961-need-help-with-navigation-with-unlimited-categoriessubcategories/#findComment-971313 Share on other sites More sharing options...
linx_effect Posted December 4, 2009 Author Share Posted December 4, 2009 OMG, I LOVE YOUUUUUUUUU it works so well...thank youuuuuu soooo much :D Link to comment https://forums.phpfreaks.com/topic/183961-need-help-with-navigation-with-unlimited-categoriessubcategories/#findComment-971328 Share on other sites More sharing options...
shedokan Posted December 4, 2009 Share Posted December 4, 2009 No problem Link to comment https://forums.phpfreaks.com/topic/183961-need-help-with-navigation-with-unlimited-categoriessubcategories/#findComment-971350 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.