Jump to content

Need Help With Navigation With Unlimited Categories/Subcategories


linx_effect

Recommended Posts

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>";
}

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

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...

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.