views:

120

answers:

1

i want to write a generalized function that can output something like this.

<span class="side_title">By Collection</span>
            <ul class="menu">
                    <li class="top">
                    <a class="top_menu" href="#url">home</a>
                    </li>
                    <li class="sub">
                    <a href="#">collectionA</a>
                    <ul>
                            <li><a href="../index.html">collectionA-1</a></li>
                            <li><a href="#url">collectionA-2</a></li>
                    </ul>
                    </li>
                    <li class="sub">
                    <a href="#">collectionB</a>
                    <ul>
                            <li><a href="#url">collectionB-1</a></li>
                    </ul>
                    </li>
                    <li class="top"><a href="#url">CollectionE</a></li>
            </ul>

            <span class="side_title">By Functions</span>
            <ul class="menu">
                    <li class="top">
                    <a class="top_menu" href="#url">home</a>
                    </li>
                    <li class="sub">
                    <a href="#">functionA</a>
                    <ul>
                            <li><a href="../index.html">functionA-1</a></li>
                            <li><a href="#url">functionA-2</a></li>
                    </ul>
                    </li>
                    <li class="sub">
                    <a href="#">functionB</a>
                    <ul>
                            <li><a href="#url">functionB-1</a></li>
                            <li><a href="#url">functionB-2</a></li>
                    </ul>
                    </li>
                    <li class="top"><a href="#url">functionE</a></li>
            </ul>

but the problem is i can't find a data structure to represent this structure, i tried the follwoing(using array - where the key is link's content, value is the address. and recursion):

$a = array (
            "By collection" => '',
            'Base' => 'thisBase',
            "expand" => array (
                    'Home' => 'home',
                    'collectionA' => 'collectionA',
                    'expand' => array (
                            'collectiona-1' => 'collectiona-1',
                            'collectiona-2' => 'collectiona-2'
                    ),
                    'collectionB' => 'collectionb',
                    'expand' => array (
                            'collectionb-1' => 'collectionb-1',
                            'collectionb-2' => 'collectionb-2'
                    ),
                    'collectionB' => 'collectionb',
                    'expand' => array (
                            'collectionc-1' => 'collectionc-1',
                            'collectionc-2' => 'collectionc-2'
                    ),
            ),
            "by Function" => ''
    );


    function expand($a=NULL)
    {
            foreach ($a as $key => $value) 
            {
                    if ($value == '')
                    {
                            echo '<span class="side_title">'.$key.'</span>';
                            echo '<ul class="menu">';
                    } elseif (is_array($value))
                    {
                            echo '<ul class="sub_menu">';
                            expand($value);
                    } else {
                            echo '<li><a href="'.$value.'>'.$key.'</a></li>';
                    }
            }
            echo '</ul>';
    }

    print_r($a);

    expand($a);

i thought about using xml/tree to represent it, but the data structure is pass between different function, so i thought it will be to much effort, so i want to know what's wrong with this? or is there better way?

A: 

I don't know, if this solves your problem, but you forgot a closing " for the href attribute.

echo '<li><a href="'.$value.'">'.$key.'</a></li>';
stefita
thanks for the help, but the problem is not there. i gave up and implement it the "dumb way"
vito huang