SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question make a tree of data unknowing how much siblings it will have??

    How to make a tree of data unknowing how much siblings it will have??
    Thats my table :


    ID--------AccID--------Parent_ID--------level
    1 ------------3-----------0 -------------0
    2------------2------------0 -------------0
    3------------202----------1 -------------1
    4------------201----------2 -------------1
    5------------20201--------3 -------------2
    6------------20202--------3 -------------2


    and i want it to look like that and more levels will be added

    * 3
    o 202
    -20201
    -20202

    * 2
    o 201

    any help please??

  2. #2
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    zalucius

  3. #3
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to use a recursive function. The 'level' column in the database is probably not needed either - that information can be got from the parent_id column.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any working examples can be helpful ???

  5. #5
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i found this good example,but it was made as a drop down list
    http://www.phpbits.info/php-tutorial...nction-in-php/

    and way to make it like that?
    HTML Code:
    <ul>
    	<li>List 0
    		<ul>
    			<li>List 0-0
    				<ul>
    					<li>item 0-0-0
    						<ul>
    							<li>new
    								<ul>
    									<li>new again
    									<li>new again
    								</ul>
    							</ul>
    						<li>item 0-0-1</li>
    					</ul>
    				</li>
    			</ul>
    		</li>
    		<li>item 0-1
    			<ul>
    				<li><a target="_new" href="HTTP://developer.yahoo.com/yui" title="go to YUI Home Page">YUI</a>
    					<ul>
    						<li>item 0-1-0</li>
    						<li>item 0-1-1</li>
    					</ul>
    				</li>
    			</ul>
    		</li>
    	</ul>
    

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,192
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    Do you need help with the query and collecting the result set or only collecting the result?

    is the result set what you posted?

  7. #7
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it did appear like a drop down list
    but the main point that i wanted it to appear in the LIST i have posted
    so it can appear as a tree
    i mean i need the data in the form of a tree which can be made using the list i have sent

  8. #8
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So, given the code you have, find where it is outputting different <select> and <option> tags, and see if you can turn it into outputting a list.

    Now you have found the example, it should be a good exercise to change it to meet your needs - post your code here if you have any trouble, but it really would be better for you to give it a go before getting someone here to do it all for you.

  9. #9
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hi Stormrider i really did try to make it spent 2 days working on it but couldn't

    i just want to display it like the list i have posted b4
    but couldn't know where to put the <ul> and <li> to get the structure i need
    every time i do it something screws up!!!!

    thats my code
    PHP Code:
    <?php
    //echo '<select name="category">';

    $allcats getTree();

    /*foreach($allcats as $key=>$value)
    {
        echo "<option value='$key'>$value</option>";
    }
    echo '</select>';*/
    echo "<div align='left'>";

    foreach(
    $allcats as $key=> $value)
    {
        echo 
    "<br />";
        echo 
    "$value";
            
    }

    echo 
    "</div>";
    /*print_r($allcats).'';
    echo count($allcats);*/

    function getTree($id=0)
    {
        static 
    $cates = array();
        static 
    $times 0;
        
    $times++;

        
    //$cates[$row['id']].="<ul>";
        //echo "SELECT * FROM accounts WHERE parent_id=$id ORDER BY parent_id<br />";
        
    $result mysql_query("SELECT * FROM accounts WHERE parent_id=$id ORDER BY id ASC");
        
    //echo $num=mysql_num_rows($result);
        
    while($row mysql_fetch_assoc($result))
        {
            
    $key=$row['id'];
            
    $active=$row['active'];
            
    //$cates[$row['id']] = ''.str_repeat("<ul>",$times-1)."<li>".$row['acc_id'].'</li>'.str_repeat("|",$times-1);
            
    $cates[$row['id']] .= ''.str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',$times);
            
    //$cates[$row['id']] .= "<a href='#' onclick='toggleMe($times)'><img src='tree/check0.gif' border='0'/></a>";
            //$cates[$row['id']] .= "<li id='showhide$times' style='display:block;'>".$row['acc_id'].'<li>';
            //$cates[$row['id']] .= "<span id='showhide$times'>";
            /*if($times==1)
            {$cates[$row['id']] .="<ul><li>";}*/
            
            
    $cates[$row['id']] .= $row['acc_id'];
            
    $cates[$row['id']] .='-'.$row['name'];
        
        
            
    /*if($times==1)
            {$cates[$row['id']] .="</ul>";}
            *///$cates[$row['id']] .='</span>';
            //$cates[$row['id']] = $times.'-'.$row['acc_id'].' '.$row['name'].' '.$row['level'];
        
        
            
    getTree($row['id']);
            
        
            
        }

        
    $times--;

        return 
    $cates;
    }

    ?>
    and thats my table structure
    PHP Code:
    CREATE TABLE IF NOT EXISTS `accounts` (
      `
    idbigint(20NOT NULL auto_increment,
      `
    acc_idint(12NOT NULL,
      `
    parent_idint(12NOT NULL,
      `
    namevarchar(255NOT NULL,
      `
    descriptiontext NOT NULL,
      `
    typeenum('credit','dept'NOT NULL,
      `
    activetinyint(4NOT NULL default '0',
      `
    levelbigint(20NOT NULL default '0',
      
    PRIMARY KEY  (`id`)
    ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=70 ;

    --
    -- 
    Dumping data for table `accounts`
    --

    INSERT INTO `accounts` (`id`, `acc_id`, `parent_id`, `name`, `description`, `type`, `active`, `level`) VALUES
    (5010149'Sab''''dept'11),
    (
    5110249'Mot''''dept'11),
    (
    521010150'Mak''''dept'12),
    (
    541020151'Ban''''dept'12),
    (
    551020251'Khaz''''dept'12),
    (
    4910'Osol''''dept'10); 

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,192
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    A algorithm first needs to be created to generate the tree. The data will never be outputted into the correct HTML hierarchy without first being a tree of objects. Objects are going to be your best friend here because they pass by reference.

  11. #11
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm i never used it b4 and not quite sure i can do it
    any other way?

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,192
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    Build the tree:

    PHP Code:
    <?php

    class AccountCollection {

        protected 
    $data;

        public function 
    __construct() {
            
    $this->data = array();
        }
        
        public function 
    add(Account $account) {
            
    $this->data[] = $account;
        }
        
        public function 
    getData() {
            return 
    $this->data;
        }

    }

    class 
    Account {

        protected 
    $id;
        protected 
    $parentId;
        protected 
    $accounts;
        
        public function 
    __construct($id,$parentId) {
            
    $this->id $id;
            
    $this->parentId $parentId;
            
    $this->accounts = new AccountCollection();
        }
        
        public function 
    getAccounts() {
            return 
    $this->accounts;
        }
        
        public function 
    getId() {
            return 
    $this->id;
        }
        
        public function 
    getParentId() {
            return 
    $this->parentId;
        }

    }

    $rows = array(
        array(
    'id'=>85,'parent_id'=>1)
        ,array(
    'id'=>6,'parent_id'=>3)
        ,array(
    'id'=>1,'parent_id'=>0)
        ,array(
    'id'=>2,'parent_id'=>0)
        ,array(
    'id'=>3,'parent_id'=>1)
        ,array(
    'id'=>4,'parent_id'=>2)
        ,array(
    'id'=>5,'parent_id'=>3)
        ,array(
    'id'=>20,'parent_id'=>5)
        ,array(
    'id'=>34,'parent_id'=>3)
        ,array(
    'id'=>21,'parent_id'=>0)
        ,array(
    'id'=>78,'parent_id'=>3)
        ,array(
    'id'=>12,'parent_id'=>78)
    );


    $accounts = new AccountCollection();
    $reserve = new AccountCollection();
    $keys = array();

    foreach(
    $rows as $row) {

        if(
    in_array($row['id'],$keys)) continue;
        
    $keys[] = $row['id'];

        if(
    $row['parent_id']===0) {
            
    $accounts->add(new Account($row['id'],0));
        } else {
            
            
    $a = new Account($row['id'],$row['parent_id']);
            if(
    handle_result($accounts,$a)!==true) {
                if(
    handle_result($reserve,$a)!==true) {
                    
    $reserve->add($a);
                }
            }
        
        }

    }

    foreach(
    $reserve->getData() as $row) {
        
        
    handle_result($accounts,$row);
        
    }

    function  
    handle_result(AccountCollection $accounts,Account $row) {

        foreach(
    $accounts->getData() as $account) {
        
            if(
    $account->getId()==$row->getParentId()) {
                
    $account->getAccounts()->add($row);
                return 
    true;
            }
            
            if(
    handle_result($account->getAccounts(),$row)===true) {
                return 
    true;
            }
            
        }

    }

    echo 
    '<pre>',print_r($accounts),'</pre>';
    ?>

  13. #13
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank u all
    i have found the solution here
    http://www.programmingtalk.com/showt...725#post100725


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •