ZFW / PHP Lists and Trees

Discussion in 'PHP' started by punkrazio, Jun 30, 2010.

  1. #1
    Hi all
    I'm going forward step by step with zend framework...
    Ive thought a database table like this:

    id - parent - title - path - description

    and some more column...
    parent rappresent the id of the father node of an element.
    I've started created a model to manage the categories...

    
    public function GetFathers()
        {
            return $this->_db->fetchAll('select * from '.$this->_name.' where parent = 0 order by title asc');
        }
        static public function GetSons($idParent)
        {
            $conn = Zend_Registry::get('db');
            return $conn->fetchAll('select * from categories where parent = '.$idParent.' order by title asc');
        }
    
    PHP:
    Here is the view i've used for test:
    <div id="categorie">
        <h1>Categorie</h1>
    <br /><br />
    <?php
    //var_dump($this->categories);
    foreach($this->categories as $cat):
        echo $this->escape($cat['title']);
        $son = application_models_Categoria::GetSons($cat['id']);
    ?>
        <br>
    <?php
    if(count($son))
        foreach($son as $figlio):
            echo '&nbsp;|___&nbsp;'.$figlio['title'].'<br />';
            //print_r($son);
        endforeach;
        ?>
        <br><br>
    <?php
    endforeach;
    ?>
    PHP:
    The function GetFathers() is called by the index controller...
    The function GetSon() is called directly from the view...
    Now, what i want to obtain is a script independent from the number of level (fathers and son), if it possible without recursive method just to save some resource, but anyway recursivity is not a big problem...
    This is an example of what i want to create:

    cat1
    |___cat3
    |___cat4
    ____|___cat7
    cat2
    cat5
    |___cat6

    I hope someone could understand me and can help me...
    Thanks in advance....
    :confused::confused::confused::confused:
     
    punkrazio, Jun 30, 2010 IP
  2. punkrazio

    punkrazio Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    heeeeelp...

    :rolleyes:
     
    punkrazio, Jul 1, 2010 IP
  3. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #3
    I haven't looked through your code in much detail but from what you are asking, it sounds like you might want to restructure your data.

    What you are trying to do is dead simple - and highly efficient - if you use a hierachal data model; do it as a binary tree instead and - although it may take a while to get used too, it will do exactly what you are asking for. I have a decent link somewhere that explains it in depth with some very good examples, should you require it, give me a shout.
     
    lukeg32, Jul 1, 2010 IP