Welcome Guest, Not a member yet? Register   Sign In
Hierarchical <ul><li> from MySql
#11

[eluser]umefarooq[/eluser]
ya coffey code will work really fine but every time you have to call sql command ever time we have query from database i worked on one code with only one query getting all parent and children and sorted properly as tree here is code

Code:
class Treeview{

    public  $counter = 0;
    protected $holder ;
    protected $menu = array();

function navigation(){

        $sql = 'select * from menu order by ordering,parent asc';
        $query = $this->db->query($sql);
        $rows = $query->result_array();
        foreach($rows as $row){
            $this->menu[$row['parent']][$row['id']] = $row;
        }
        $this->tree();

    }

    function tree($child=array(),$space=''){
        $level = $this->counter;

        if(isset ($this->menu[0])&& is_array($this->menu[0]) || (is_array($child) && !empty ($child))){
            $this->counter++;
            $this->holder[$this->counter] = (is_array($child) && !empty ($child))?$child:$this->menu[0];

            foreach ($this->holder[$this->counter] as $v){

                echo $space.$v['name'].'<br />';

                if(isset($this->menu[$v['id']])){

                   if($level)
                    $this->tree($this->menu[$v['id']],'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<sup>L</sup>');
                    else
                    $this->tree($this->menu[$v['id']],'&nbsp;&nbsp;&nbsp;<sup>L</sup>');
                }
            }

        }

    }
}

code is generating proper tree, but i was not able to put in ul and li any body can do it that's really good for CI community
#12

[eluser]gtlitc[/eluser]
I really dig the idea of using a recursice function to build my site menus but I have am having a problem and have been banging my head for ages now.
I need my menu function to return a nested list as per the above examples but I dont want non-active irelevent elements of the tree to be displayed.

For example here is an example menu with all elements shown:
Code:
<ul>
    <li><a href="1.html">link 1</a>
        <ul>
            <li><a href="1-1.html">link 1-1</a>
            <li><a href="1-2.html">link 1-2</a>
        </ul>
    </li>
    <li><a href="2.html">link 2</a>
        <ul>
            <li><a href="2-1.html">link 2-1</a>
            <li><a href="2-2.html">link 2-2</a>
        </ul>
    </li>
    <li><a href="3.html">link 3</a></li>
</ul>

But if the current page is 1-2.html I want to have a menu like this:

Code:
<ul>
    <li><a href="1.html">link 1</a>
        <ul>
            <li><a href="1-1.html">link 1-1</a>
            <li><a href="1-2.html">link 1-2</a>
        </ul>
    </li>
    <li><a href="2.html">link 2</a></li>
    <li><a href="3.html">link 3</a></li>
</ul>

Evidently I would pass either the ID or the name of the current page to the Menu function.
Any ideas? Anyone? Please help me. You may be the only one that can help me now?




Theme © iAndrew 2016 - Forum software by © MyBB