SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Needed implementing this simple Class

    I found a Modified Preorder Traversal Class here: http://www.aesthetic-theory.com/learn.php?mptt
    Unfortunately, examples of the class weren't provided. I'm an OOP noob. Based on the class, could someone look at the class and tell me how to use it? I would really appreciate it. Thanks in advance!
    //db structure
    Code:
      CREATE TABLE `data` ( `id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, 
    `title` VARCHAR(200) NOT NULL, 
    `left_id` INT UNSIGNED NOT NULL, 
    `right_id` INT UNSIGNED NOT NULL, 
    `level` MEDIUMINT UNSIGNED NOT NULL, 
    UNIQUE (`id`) );
    PHP Code:
    <?php
    class Tree

    {

        var 
    $data_array// we'll store the data in this

        
    var $cnx// our connection to the database



        // our constructer, we'll use it to connect to the database

        
    function Tree()

        {

            
    $cnx mysql_connect(DB_HOSTDB_USERNAMEDB_PASSWORD) or die ('Unable to connect to database');

            
    mysql_select_db(DB_DATABASE$cnx) or die ('Unable to select database');

        }



        
    /* this will display the data... we'll give it a few paramaters

         * parent: this will be an integer or false,

         *        if its an integer we will get all children,

         *        if its false we will get the entire tree

         * dig_levels: this will be an integer, or false

         *        represents the number of levels to dig BELOW PARENT for data.

         *        Examples, if its 3 and we get the entire tree, we'll get everything with a level <= 3

         *            (music,weezer,pinkerton BUT NOT elscorcho)

         *        ex: if its 2 and we have parent weezer (level:2) we'll get 2 + dig_level, or everything with a level <= 4

         * inc_parent: whether or not to include parent in result set

        */

        
    function get_data($parent false$dig_level false$include_parent true)

        {

            if(
    $parent)

            {

                
    $sql 'SELECT `right_id`, `left_id`, `level` FROM ' DATA_TABLE ' ';



                
    $where 'WHERE `id`=' $parent ';';

                
    $parent_res mysql_query($sql $where) or die(mysql_error());

                unset(
    $sql$where);

                if(
    $include_parent) {

                    
    $lt '<=';

                    
    $gt '>=';

                }

                else

                {

                    
    $lt '<';

                    
    $gt '>';

                }

                
    $parent_arr mysql_fetch_array($parent_res);

                
    //$parent_right = $parent_arr[0];

                //$parent_left = $parent_arr[1];

                //$parent_nest = $parent_arr[2];

                
    $where 'WHERE `left_id` ' $gt ' ' $parent_arr[1] . ' AND `right_id` ' $lt ' ' $parent_arr[0] . ' ';

                unset(
    $parent_res);

            }

            if(
    $dig_level)

            {

                
    $nest_limit = isset($parent_arr) ? $parent_arr[2] + $dig_level $dig_level;

                
    $level_adjustment $nest_limit $dig_level// we'll use this later

                
    if(isset($where))

                {

                    
    $where .= 'AND `level` <= ' $nest_limit ' ';

                }

                else

                {

                    
    $where 'WHERE `level` <= ' $nest_limit ' ';

                }

            }

            
    $sql 'SELECT * FROM `' DATA_TABLE '` ' $where 'ORDER BY `left_id` ASC';

            unset(
    $where$parent_arr);

            
    $raw_result mysql_query($sql) or die(mysql_error());

            while(
    $item mysql_fetch_array($raw_result)) {

                
    $data[$item['id']]['title'] = $item['title'];

                
    $data[$item['id']]['left_id'] = $item['left_id'];

                
    $data[$item['id']]['right_id'] = $item['right_id'];

                
    $data[$item['id']]['level'] = $item['level'];

                
    $data[$item['id']]['relative_level'] = $item['level'] - $level_adjustment;

            }

            unset(
    $raw_result);

            
    $this->data_array $data;

            return 
    $data;
        }
    }

    ?>
    Last edited by bruin03; Apr 4, 2005 at 10:10. Reason: incomplete post

  2. #2
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would need to know which parameters go inside $tree->get_data(...).
    That's all. I'm not sure if i'm doing it correctly. Thanks.

  3. #3
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Oxford, UK
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The comments say it all really...

    parent: this will be an integer or false, if its an integer we will get all children, if its false we will get the entire tree.
    dig_levels: this will be an integer, or false, represents the number of levels to dig BELOW PARENT for data.
    inc_parent: whether or not to include parent in result set.

    So if you want to get the two branches/levels below a node which has an ID of 4, and want to show the parent node, use this:

    PHP Code:
    get_data(42true); 


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
  •