Recursive menutree from mysql

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • replace-this-with-my-name@detandetfirma.dk

    #1

    Recursive menutree from mysql

    Hi.

    How do I return a string containing an entire menu-tree from a
    recursive function?

    Here is my current recursive function:

    function page_tree( $_i ){

    //Call global mysql connection variable
    global $_conn;

    //SQL statement
    $_sql = "SELECT page_tree . * , document . * FROM page_tree
    INNER JOIN document ON document.d_id = page_tree.p_doc ument_id
    WHERE (page_tree.p_pa rent_id = ".$_i.")
    ORDER BY page_tree.p_id" ;

    //Query
    $_q = mysql_query( $_sql, $_conn );

    //Check for number of returned rows
    if( mysql_num_rows( $_q ) )
    {

    //While there is content
    while( $_r = mysql_fetch_arr ay( $_q ) )
    {
    print "<a href=". $_r['d_file_name'] .">".$_r['d_link_text']."</a>";
    //Recursive function call
    page_tree( $_r['p_id'] );
    }
    }
    //Release query
    mysql_free_resu lt( $_q );
    }//page_tree( $_i )

    This will generate a file list of all the content in the db in a
    tree-like structure.

    My Problem is that what I want should look more like this:

    <ul>
    <li>Front page</li>
    <li>Second page
    <ul>
    <li>Second page, 1. sublevel</li>
    <li>Second page, 2. sublevel
    <ul><li>Secon d page, 2. sublevel</li></ul>
    </li>
    <li>Second page, 1. sublevel</li>
    </lu>
    </li>
    <li>Second page</li>
    </ul>

    How do I go about returning a string that looks like the one above?
    The recursive idea is no demand, it's just an idea.

  • Joachim Weiß

    #2
    Re: Recursive menutree from mysql

    replace-this-with-my-name@detandetfi rma.dk schrieb:

    give your functione another parameter:
    [color=blue]
    > function page_tree( $_i ){[/color]
    function page_tree( $_i ,$level=0){
    ....

    now the stuff for the level:[color=blue]
    > if( mysql_num_rows( $_q ) )[/color]
    I'd suggest: echo('<ul class="level$le vel">')


    same with the loop sth like:[color=blue]
    > while( $_r = mysql_fetch_arr ay( $_q ) )
    > {
    > print "<a href=". $_r['d_file_name'] .">".$_r['d_link_text']."</a>";[/color]
    echo('li class="level$le vel" <a href .... '


    and now the trick.....
    [color=blue]
    > //Recursive function call
    > page_tree( $_r['p_id'] );[/color]

    use:
    page_tree( $_r['p_id'] , $level +1);

    HIH

    Jo

    Comment

    • replace-this-with-my-name@detandetfirma.dk

      #3
      Re: Recursive menutree from mysql

      Thank you for the reply.

      But it's not quite what I need. I need a function that will return a
      string containing the menu setup shown above.

      Comment

      Working...