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?