Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Récursivité du menu

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: French
View previous topic :: View next topic  
Author Message
Tr0p1caL
Smarty n00b


Joined: 05 Feb 2015
Posts: 2

PostPosted: Thu Feb 05, 2015 9:33 pm    Post subject: Récursivité du menu Reply with quote

Bonjour à tous,

Voici mon tableau de menu:
Code:

Array (
     [0] => Array (
             [id] => 1
             [children] => 0
             [title] => Accueil
             [slug] =>
             [position] => 1
     )
     [1] => Array (
             [id] => 3
             [children] => 2
             [title] => Suisse
             [slug] => suisse
            [position] => 1
     )
     [2] => Array (
             [id] => 2
             [children] => 0
             [title] => Contact
             [slug] => contact
             [position] => 99 )
     )


Mon objectif est le suivant:
Code:
<ul>
<li><a href="" title="#">Accueil</a></li>
<li><a href="contact" title="#">Contact</a>
    <ul>
        <li><a href="contact-suisse"> suisse</a></li>
    </ul>
</li>
</ul>


Et voici mon code smarty:

Code:

<ul>
   {function name=menu level=0}
       {foreach $data as $element}
           <li>{$element.nom}</li>
           {if $element.children}
              <ul>{menu data=$element.children level=$level+1}</ul>
           {/if}
       {/foreach}
    {/function}

    {menu data=$navigations}
</ul>


Comme retour, j'ai simplement une page blanche, sans code, ni même celui qui est autour du <ul></ul>.

Sauriez-vous m'aider?
Merci à vous !
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Feb 06, 2015 1:20 am    Post subject: Reply with quote

If I were you, I'd change your array to be more like
Code:
Array (
    [0] => Array (
        [id] => 1
        [children] => null
        [title] => Accueil
        [slug] =>
        [position] => 1
    )
    [1] => Array (
        [id] => 3
        [children] => Array (
            [0] => Array (
                [id] => 2
                [children] => null
                [title] => Contact
                [slug] => contact
                [position] => 99
            )
        )
        [title] => Suisse
        [slug] => suisse
        [position] => 1
    )
)

Then template code become as straightforward as
Code:
{foreach $data as $element}
  {if $element@first}<ul>{/if}
    <li><a href="{$element.slug}">{$element.title}</a>{if $element.children}
      …reiterate…{/if}</li>
  {if $element@last}</ul>{/if}
{/foreach}


With your approach, you'd have to keep track of already rendered nodes.
Back to top
View user's profile Send private message
Tr0p1caL
Smarty n00b


Joined: 05 Feb 2015
Posts: 2

PostPosted: Fri Feb 06, 2015 8:02 am    Post subject: Reply with quote

Hello,

thanks for your answer. How can I change my array? in my php code:

Code:


function load_navigation() {
            try {
                $query = $this->_db->selectAllOrderBy("sys_navigation", "position");
                $query->execute();
                $navigations = array();
               
                while($row = $query->fetch(\PDO::FETCH_ASSOC)) {
                    $navigations[] = array(
                       'id' => $row['id'],
                       'children' => $row['children'],
                       'title' => $row['title'],
                       'slug' => $row['slug'],
                       'position' => $row['position']
                    );
                }
                return $navigations;
            } catch(PDOException $e) {
                throw new Exception($e->getMessage());
            }
    }

[/code]
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Feb 06, 2015 11:32 am    Post subject: Reply with quote

Change your database structure, so that children refer to parent, rather than parent refer to children.
Else you will need to run over your dataset twice at least to find all top-level elements.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: French All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP