Re: [SMARTY] Re: this mysql query with smarty

From: Date: Wed, 17 Mar 2004 19:54:54 +0000
Subject: Re: [SMARTY] Re: this mysql query with smarty
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-11143@lists.php.net to get a copy of this message
I'm googling for days but no solution. I manage to display categosries and subcategories using simple mysql query and sort data in template.
The project I'm working on does exactly this, you have categories, subcategories a parentID and a position parameter. I added the position parameter later because It was important for me to be able to decide where each category (or subcategory) should be, and not simply in the order they're introduced into the database. Anyway, in the PHP I have a function (I'm using classes) thats called makeTree(), this function unpacks from the database and gets everything in a nice array. When it unpacks it it sets it's Position parameter as the key. So the position parameters go from 0 and up. Then any children it will unpack in a children array, again using the position as the key. By using the position parameter as key, I end up with a numeric index that corresponds to the position they should be in the array. Array (
    [0] => Array
        (
            [id] => 2
            [name] => Name
            [type] => title
            [url] =>
            [parentID] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [name] => Name
                            [type] => preferences
                            [url] => name/
                            [parentID] => 2
                        )
                    [1] => Array
                        (
                            [id] => 5
                            [name] => Details
                            [type] => preferences
                            [url] => details
                            [parentID] => 2
                        )
                )
        )
    [1] => Array
        (
            [id] => 3
            [name] =>
...etc. Then in smarty I use two nested foreach loops to display the data:
    <ul>
    {foreach name=category from=$categories key=catkey item=category}
        <li class="category">{$category.name}</li>
        {foreach name=subcategories from=$category.children key=subcatkey item=subcategory}
            <li class="subcategory"><a href="http://{$serverName}/{$username}/{$subcategory.url}">{$subcategory.name}</a></li>
        {/foreach}
    {/foreach}
    </ul>
That way I get a nice list, the class determines if it's a category or subcategory, so in my CSS I have subcategories indented. Vonleigh Simmons [vanlei] <www.illusionart.com/pi/>

« previous php.smarty.general (#11143) next »