By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

Recursive menutree from mysql

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.