Infinite sub-catagories

This is a discussion on "Infinite sub-catagories" within the PHP Forum section. This forum, and the thread "Infinite sub-catagories are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jun 28th, 2008, 17:46
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,255
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
Infinite sub-catagories

Once again, aso got me thinking. Let's say I have a database full of sentences. A sentence may be the child of another sentence, but the parent sentence may also be a child. So you have virtually infinite sub-sentences. Now how could you take that, and print it out as a list?
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jul 2nd, 2008, 22:50
Junior Member
Join Date: Jun 2008
Location: Nowhere
Age: 21
Posts: 15
Thanks: 2
Thanked 1 Time in 1 Post
Re: Infinite sub-catagories

It's easy to represent in a database, but not so fun to parse with PHP. In the databse, you need (at least) 3 columns: sid (sentence id), pid (parent id), sentence

Each sentnece should have a unique sid. If the sentence is a root node, set pid to NULL. Otherwise, set it to it's parent's sid. "sentence" obviously stores the sentence.

Now....to parse this bugger. Off the top of my head, here's a highly inefficient algorithm:

Select all sentences with a NULL pid. For each of these, select all sentences that have a pid equal to its sid...... I hope you see where this is going. Do recursion.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jul 2nd, 2008, 23:00
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,255
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
Re: Infinite sub-catagories

I've already got the database thing down, the PHP I'm not sure how to do. Is it possible to access my_func() from within my_func()?
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jul 2nd, 2008, 23:03
Junior Member
Join Date: Jun 2008
Location: Nowhere
Age: 21
Posts: 15
Thanks: 2
Thanked 1 Time in 1 Post
Re: Infinite sub-catagories

Yes, that's called recursion.

Edit: Just make sure to escape when no more pid's are found or you can easily get yourself into an infinite loop.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jul 2nd, 2008, 23:08
Junior Member
Join Date: Jun 2008
Location: Nowhere
Age: 21
Posts: 15
Thanks: 2
Thanked 1 Time in 1 Post
Re: Infinite sub-catagories

Something like this (untested):

Code: Select all
function f($pid)
{
$result = mysql_query("SELECT * FROM sentences WHERE pid=$pid");
echo "<ul>";
while($row = mysql_fetch_assoc($result)) {
echo "<li>".$row['sentence']."</li>";
f($row['sid']);
}
echo "</ul>";
}

f('NULL');
Edit: Hey, add the code tags to the toolbar. How else am I supposed to know if these forums support them? Every forum likes to adopt their own standards >.<

Last edited by darkzerox; Jul 2nd, 2008 at 23:39. Reason: Code tags, please, darkzerox
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

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

BB 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
Infinite Loop Bradster PHP Forum 2 May 31st, 2007 08:18


All times are GMT. The time now is 16:06.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42