TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-10-2008, 06:29 AM   #1 (permalink)
The Wanderer
 
Join Date: Jun 2008
Posts: 19
Thanks: 0
oscargodson is on a distinguished road
Red face How do I make a tree view from MySQL and PHP

I have an application in the making that needs to show a treeview of files and directories.

I have it all working based on an <ul>. Each <li> is a sub file .

I have a table set up with:
name, parent, type, user, content, project, date, id

I set up the DB to set the parent as the id of it's parent (e.g. Directory "A" has an id of "2" and parent of "0" and directory "B" has an id of "20" and a parent of "2". B would be nested inside of A because it's parent matches it's id).

Here is a sample of what I want the markup to be like:

HTML Code:
<ul>
	<li class="folder">
		The Project Name
		<ul>
			<li class="folder">
				css
				<ul>
					<li class="file">style.css</li>
					<li class="file">thickbox.js</li>
				</ul>
			</li>
			<li class="folder">
				includes
				<ul>
					<li class="folder">
						functions
						<ul>
							<li class="file">math.php</li>
							<li class="file">mysql_connect.php</li>
						</ul>
					</li>
					<li class="file">header.php</li>
					<li class="file">sidebar.php</li>
					<li class="file">footer.php</li>
				</ul>
			</li>
		</ul>
	</li>
</ul>
I can not figure this out. Does anyone know how to sort recursively like this?!
oscargodson is offline  
Reply With Quote
Old 12-10-2008, 10:03 AM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

What do you currently have?
__________________
Tanax is offline  
Reply With Quote
Old 12-10-2008, 10:49 AM   #3 (permalink)
The Wanderer
 
Join Date: Jun 2008
Posts: 19
Thanks: 0
oscargodson is on a distinguished road
Default

I actually got it thanks to a guy over at tizag.com. He wrote me a small function that worked right out of the box. Just in case anyone else needs to make a site tree of some sort using a ul (could be an ol of course):

PHP Code:

function tree_view($index)
{
    
$q=mysql_query("SELECT * FROM table_name WHERE parent=$index");
    if(!
mysql_num_rows($q))
        return;
    echo 
'<ul>';
    while(
$arr=mysql_fetch_assoc($q))
    {
        echo 
'<li>';
        echo 
$arr['name'];//you can add another output there
        
tree_view($arr['id']);
        echo 
'</li>';
    }
    echo 
'</ul>';

then simply call it with:
PHP Code:
tree_set(0); 
Worked like a charm!
oscargodson is offline  
Reply With Quote
Old 12-10-2008, 02:22 PM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Looks good, could be improoved, but I guess it works
Though, I think you made a typo: tree_set(0) should be tree_view(0)
__________________
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP & MySQL Inserting multiple entires from range. Acrylic Absolute Beginners 3 10-02-2008 02:27 AM
how to call mysql procedures in php??? gptArun Absolute Beginners 7 06-05-2008 08:13 PM
Securing your MySQL Queries with Sprintf Wildhoney General 26 03-18-2008 06:52 PM
Rating System: AJAX, MYSQL, PHP Orc General 6 02-26-2008 05:09 PM
Error in connecting to MySQL via PHP EyeDentify MySQL & Databases 0 01-03-2008 01:06 PM


All times are GMT. The time now is 06:57 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design