2

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

1
  • Just get the whole table, sorted by parent ID and build the hierarchy from that. Commented Mar 6, 2011 at 20:55

2 Answers 2

2
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>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

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: Create an array, push the the current ID to the end of it, and pass it along to the children.
-1

You're not opening and closing your UL tags

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.