Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

here is the html structure how it should look like

<li><a href="#" class="menulink">Dropdown One</a>
    <ul>
        <li><a href="#">Navigation Item 1</a></li>
        <li>
            <a href="#" class="sub">Navigation Item 2</a>
            <ul>
                <li class="topline"><a href="#">Navigation Item 1</a></li>
                <li><a href="#">Navigation Item 2</a></li>
            </ul>
        </li>
        <li>
            <a href="#" class="sub">Navigation Item 3</a>
            <ul>
                <li class="topline"><a href="#">Navigation Item 1</a></li>
                <li><a href="#">Navigation Item 2</a></li>
                <li>
                    <a href="#" class="sub">Navigation Item 3</a>
                    <ul>
                        <li class="topline"><a href="#">Navigation Item 1</a></li>
                        <li><a href="#">Navigation Item 2</a></li>
                        <li><a href="#">Navigation Item 3</a></li>

                    </ul>
                </li>
                <li><a href="#">Navigation Item 4</a></li>
            </ul>
        </li>
        <li><a href="#">Navigation Item 4</a></li>
        <li><a href="#">Navigation Item 5</a></li>
    </ul>
</li>
<li><a href="#" class="menulink">Dropdown Two</a>
    <ul>
        <li><a href="#">Navigation Item 1</a></li>
        <li><a href="#">Navigation Item 2</a></li>
    </ul>
</li>

and here is the mysql table

id |parent_id|        name     |
---+---------+-----------------+
 1 |    0    |Dropdown One     |
 2 |    1    |Navigation Item 1|
 3 |    1    |Navigation Item 2|
 4 |    3    |Navigation Item 1|
 5 |    3    |Navigation Item 2|
 6 |    1    |Navigation Item 3|
 7 |    6    |Navigation Item 1|
 8 |    6    |Navigation Item 2|
 9 |    6    |Navigation Item 3|
10 |    6    |Navigation Item 4|
11 |    1    |Navigation Item 4|
12 |    1    |Navigation Item 5|
13 |    0    |Dropdown Two     |
14 |   13    |Navigation Item 1|
15 |   13    |Navigation Item 2|

maybe not the best approach but here is my PHP function attempt:

function listCategory($parent_id,$level=0) {
    $query = "SELECT name, id , parent_id FROM category  WHERE  parent_id=".$parent_id;
    $res = mysql_query($query) or die($query);
    $i=1;
    while (list ($name, $id) = mysql_fetch_row($res))
    {   
        if ($level==0) {echo '<li><a href="#" class="menulink">'.$name.'</a></li>';}

        echo '<li><a href="#" class="menulink">'.$name.'</a></li>';
        //echo $name ." i: ".$i." level: ".$level."<br>";
        $i++;
        listCategory($id,$level+1);
    }
}

listCategory(0);

thanks for any help in advance

share|improve this question
    
Just get the whole table, sorted by parent ID and build the hierarchy from that. –  Oded Mar 6 '11 at 20:55

2 Answers 2

up vote 2 down vote accepted
function listCategory($parent_id,$level=0) {
    $query = "SELECT name, id , parent_id FROM category  WHERE  parent_id=".$parent_id;
    $res = mysql_query($query) or die($query);
    if(mysql_num_rows($res) == 0) return;
    echo '<ul>';
    while (list ($name, $id) = mysql_fetch_row($res))
    {   
        if ($level==0)
        {
           echo '<li><a href="#" class="menulink">'.$name.'</a>';
        }
        else
        {
           echo '<li><a href="#">'.$name.'</a>';
        }
        listCategory($id,$level+1);
        echo '</li>';
    }
    echo '</ul>';
}
share|improve this answer
    
thank you! worked fine! one more quetion: how could i keep track of the items hierarchy? so id1 = 1, id2 = 1 1,id3=1 2, id4= 1 2 1, id5=1 2 2 ... –  PCD25 Mar 6 '11 at 23:10
    
@PCD25: Create an array, push the the current ID to the end of it, and pass it along to the children. –  Tim Cooper Mar 6 '11 at 23:15

You're not opening and closing your UL tags

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.