Recursive menutree from mysql

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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_document_id
WHERE (page_tree.p_parent_id = ".$_i.")
ORDER BY page_tree.p_id";

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

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

//While there is content
while( $_r = mysql_fetch_array( $_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_result( $_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:

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

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.

Re: Recursive menutree from mysql schrieb:

give your functione another parameter:

Quoted text here. Click to load it
function page_tree( $_i ,$level=0){

now the stuff for the level:
 > if( mysql_num_rows( $_q ) )
I'd suggest: echo('<ul class="level$level">')

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

and now the trick.....

Quoted text here. Click to load it

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



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.

Site Timeline