469,330 Members | 1,284 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

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_document_id
WHERE (page_tree.p_parent_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_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:

<ul>
<li>Front page</li>
<li>Second page
<ul>
<li>Second page, 1. sublevel</li>
<li>Second page, 2. sublevel
<ul><li>Second 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.

Jul 17 '05 #1
2 2857
re***********************@detandetfirma.dk schrieb:

give your functione another parameter:
function page_tree( $_i ){ 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.....
//Recursive function call
page_tree( $_r['p_id'] );


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

HIH

Jo
Jul 17 '05 #2
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.

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Jon Slaughter | last post: by
5 posts views Thread by Joe Stump | last post: by
9 posts views Thread by seberino | last post: by
3 posts views Thread by Robertico | last post: by
3 posts views Thread by from.future.import | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.