473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3102
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
2
by: Steven Burn | last post by:
..:: The Specs: MS Access 2000 (host charges extra for SQL/MySQL) MS Windows Server 2003 (prod) / MS XP SP1 (dev) ..:: The setup: The database has been setup with two tables; tblDownloads
5
by: Joe Stump | last post by:
I have a table that will have a parent/child relationship (specifically a recursive collection of categories) and was wondering if I can reference a key in the same table ... CREATE TABLE...
2
by: jeyakumar | last post by:
Hi I want some example using recursive
9
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
3
by: Robertico | last post by:
I'am new to php and have a question about a recursive file search. I'd like to do a recursive search for jpg-files and add the filenames (full path) to a mysql database. I appreciate any help ! ...
0
by: champ1979 | last post by:
I wrote an algorithm to get all the relatives of a person in a family tree. I'm basically getting all the users from the DB and am doing the recursive logic in code, so that there is only 1 call...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
1
by: bipinskulkarni | last post by:
How to write recursive stored procedure in mysql?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.