General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 5th, 2002, 09:22 PM
marcoBR marcoBR is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 4 marcoBR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Tree Structure Question.

Hi!

Currently I'm using a tree like sections structure, e.g.

Code:
Table: sections
+--------+------------+----------+
| sec_id | sec_parent | sec_name |
+--------+------------+----------+
|      1 |          1 |     Home |
|      2 |          1 |		News |
|      3 |          2 |   Sports |
|      4 |          3 |   Soccer |
|      5 |          4 |	   Teams |
|      6 |          5 |	 Arsenal |
|      7 |          1 |	   World |
|      8 |          7 |	 England |
|      9 |          8 |	 Economy |
+--------+------------+----------+


Also I'm using the function below to cover the tree:

PHP Code:
function cover_tree(){

        
$query "select * from sections";
        
        
$result mysql_query($query);
        
        
$i=1;
        
        while(
$row mysql_fetch_array($result)){
            
$sec_id[$i] = $row['sec_id'];
            
$sec_parent[$i] = $row['sec_parent'];
            
$sec_name[$i] = $row['sec_name'];
            while(
$sec_parent[$i] > 1) {      
                
$query "select * FROM sections where sec_id = $sec_parent[$i]";
                
$subresult mysql_query($query);
                
$subrow mysql_fetch_array($subresult);
                
$subsec_parent[$i] = $subrow['sec_parent'];
                
$subsec_name $subrow['sec_name'];
                
$sec_name[$i] = $subsec_name." > ".$sec_name[$i];
            }
        
$i++;
        }
        
        if(
$sec_name){
            for(
reset($sec_name); $key=key($sec_name); next($sec_name)) {
                
$sections_tree[$sec_id[$key]] = $sec_name[$key];
            }
        }
        
        return 
$sections_tree;
    } 


The above function will return:
------------------------------------
Home
News
News > Sports
News > Sports > Soccer
News > Sports > Soccer > Teams
News > Sports > Soccer > Teams > Arsenal
World
World > England
World > England
World > England > Economy
------------------------------------


I would like to see only subsections below the section Sports, (sec_id = 3), thus my sections_tree should be represented,

--------------------------------------
Sports
Sports > Soccer
Sports > Soccer > Teams
Sports > Soccer > Teams > Arsenal
--------------------------------------

The question is:

How could I improve this function to show only subsections below a pre-determined section???

Any ideas!?!?


Kindest Regards,

marcoBR


BTW, sorry 4 my english ...

Last edited by marcoBR : December 5th, 2002 at 09:42 PM.

Reply With Quote
  #2  
Old December 6th, 2002, 12:19 PM
dgrigg dgrigg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: Toronto, ON, Canada
Posts: 10 dgrigg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
>>I would like to see only subsections below the section Sports, (sec_id = 3), thus my sections_tree should be represented

An easy way would be to run a query to get the child records of the highest level Sec_ID you want

SELECT * FROM SECTIONS WHERE Parent_ID = $SecID;
and put those records into an array.

Use a loop to go through the array, taking the Sec_ID from each element and running the query to find the child records of each element and put them into the array until all children have been found.

Once you have found all the records and put them in the array, use a loop to get them out.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Tree Structure Question.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT