Jump to content

Help with infinite category


irkevin

Recommended Posts

Hi,

 

I have a function that display an infinite list of categories.. So far this work fine.. But now I want to display only the second level of every main category and not the subs.. Consider this:

 

Main1                Main2                Main3                Main4

  car1                  Bike1                watch1              Game1

    SubSub1            SubSub1            SubSub1            SubSub1

  car2                  Bike2                  watch2            Game1

    SubSub2            SubSub2              SubSub2            SubSub2

 

What I want to achieve is, display on

 

Car1

Car2

Bike1

Bike2

Watch1

Watch2

Game1

Game2

 

Here is the code I have so far:

 

function get_cat()
{
	db_connect();

	$sql = "SELECT * FROM category";

	$result = mysql_query($sql);

	if(mysql_num_rows($result) > 0){
		while($row = mysql_fetch_array($result)){
			$menu_array[$row['id']] = array(
				'name' => $row['name'],
				'parent' => $row['parent_id'],
				'id' => $row['id']
			);
		}	

		return $menu_array;
	}	
}

function generatemenu($parent,$array){
	$has_childs = false;

	//this prevents printing 'ul' if we don't have subcategories for this category

	//use global array variable instead of a local variable to lower stack memory requierment

	foreach($array as $key => $value) {

		if ($value['parent'] == $parent){       
				$has_childs = true;

				if($has_childs == true){

					if($value['parent'] == 0)
						echo '<ul class="main">';
					else
						echo '<ul class="subs">';
				}
				//if this is the first child print '<ul>'              
				if($value['parent'] == 0)         
					echo '<li><a href="index.php?category='.$value['id'].'">' . $value['name'] . '</a>';
				else
					echo '<li><a href="index.php?category='.$value['id'].'">' . $value['name'] . '</a>';
				generatemenu($key ,$array);
				//call function again to generate nested list for subcategories belonging to this category
				echo '</li>';

			if($has_childs == true)
				echo '</ul>';
		}

	}
}

 

Can someone help?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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