SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    tree menu recursive function ul li

    Hi

    I found a function on this board some time ago.

    CREATE TABLE `menu` (
    `menu_id` tinyint(2) NOT NULL auto_increment,
    `menu_name` char(200) NOT NULL,
    `parent_id` tinyint(2) NOT NULL,
    `menu_order` tinyint(2) NOT NULL,
    PRIMARY KEY (`menu_id`)
    )


    PHP Code:
    function getNodes($menu_id$level 0

        
    $sql "SELECT menu_id, menu_name, parent_id 
               FROM menu WHERE parent_id=
    $menu_id"

        
    $result =  mysql_query($sql); 
         
        while (
    $row mysql_fetch_array($result)) 
        {   
            echo 
    str_repeat('---'$level) . ' ID: ' $row['menu_id'] . 
            
    ', name: ' $row['menu_name'] . '<br />'
                     
        
    // call the same function recursively 
        
    getNodes($row['menu_id'], $level+1); 
        } 


    getNodes(0);  // pass the ID of the menu item where to begin from as parameter (0=root in my case) 
    I would like to print out this menu in <ul><li> style

    i tried something like this, but its not quite as i want it

    PHP Code:
    <ul>
    <?
    connectDB
    ($host$dbUser$dbPass$dbName);

    function 
    getNodes($menu_id$level 0

        
    $results queryDB("SELECT menu_id, menu_name, parent_id FROM menu2 WHERE parent_id=$menu_id"); 
        

        while (
    $row mysql_fetch_array($results)){  
            if(
    $level 0){
                
    $li_start"\t<ul>\n";
                
    $li_end ="\t</ul>\n";
            } else {
                
    $li_start"\t<li>\n";
                
    $li_end ="\t</li>\n";
            } 
            echo 
    $li_start.str_repeat("\t\t<li>"$level) . $row['menu_name'] . str_repeat("\t\t</li>"$level).$li_end;
         
                         
            
    // call the same function recursively 
            
    getNodes($row['menu_id'], $level+1); 
        } 
        

    getNodes(0);  // pass the ID of the menu item where to begin from as parameter (0=root in my case) 
    closeDB(); 
    ?>
    </ul>
    thx

    Chris

  2. #2
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?

    Chris

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you said its not what you want.

    but you did not say why its not what you want, nor did you say what you do want.

  4. #4
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok sorry let me clarify

    i want the recursive function to write the html like this
    Code:
    <ul>
    	<li>Item 1
    		<ul>
    			<li>Item 1.1</li>
    			<li>Item 1.2</li>
    			<li>Item 1.3</li>
    		</ul>
    	</li>
    	<li>Item 2
    		<ul>
    			<li>Item 2.1</li>
    			<li>Item 2.2</li>
    		</ul>
    	</li>
    	<li>Item 3
    		<ul>
    			<li>Item 3.1</li>
    		</ul>
    	</li>
    	<li>Item 4</li>
    	<li>Item 5</li>
    </ul>
    thx

    Chris


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •