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

Rekursion mit Smarty 3

 
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: German
View previous topic :: View next topic  
Author Message
eazytrader
Smarty Rookie


Joined: 16 Jul 2010
Posts: 33

PostPosted: Wed Feb 27, 2013 7:07 am    Post subject: Rekursion mit Smarty 3 Reply with quote

Hallo Leute,

ich habe eine Aufgabe die anscheinend unlösbar ist "für mich".
Und zwar versuche ich einen Weg zu finden wie man mit Smarty 3 folgende rekursive Methode realisieren kann.

Code:
        public function listCategory($parent_id = 0, $level = 0){

            $menue = '<ul>';

            foreach($this->createarray($parent_id) as $value){
                if ($level==0)
                {
                   $menue .=  '<li><a href="#" class="menulink">'.$value['categories_id'].'</a>';
                }
                else
                {
                   $menue .=  '<li><a href="#">'.$value['categories_id'].'</a>';
                }
                $menue .= $this->listCategory($value['categories_id'],$level+1);

                $menue .=  '</li>';
            }
            $menue .=  '</ul>';

            return $menue;

        }


So gut kenne ich mich mit Smarty leider noch nicht aus.
Hätte mir vielleicht jemand einen Vorschlag?

Vielen herzlichen Dank.
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Wed Feb 27, 2013 8:39 am    Post subject: Reply with quote

Das hat ja jetzt nicht wirklich viel mit Smarty zu tun.

Ich nehme mal an du willst eine Navigation mit Unterpunkten erstellen. Ich habe das z. B. so gemacht, ich übergebe an Smarty mein Navigations-Objekt über

Code:
$smarty->assign("menus", $this);


Im Template rufe ich dann die Methode "GetParent" auf

Code:

{foreach from=$menus->GetParent() item=menu}
{* tu dinge*}
{assign var="subMenus" value=$menus->GetChild($menu.ID)}
                {if $subMenus != false}
{* tu dinge für unterpunkte*}
{/if}
{foreach}



und diese ruft dann die "GetChild"-Methode auf und übergibt die ID des Parents. Bei mir waren die Daten allerdings in der Datenbank, was aber keinen Unterschied macht, wil die Daten aus der DB auch als Array zurückkamen.
Back to top
View user's profile Send private message Visit poster's website
eazytrader
Smarty Rookie


Joined: 16 Jul 2010
Posts: 33

PostPosted: Wed Feb 27, 2013 9:16 am    Post subject: Reply with quote

Danke für die Antwort.
Es geht mir halt darum wie ich es hinbekomme, das Menü in meinem Template darzustellen.

Bei mir kommen die Einträge auch aus der Datenbank.
Funktioniert deine Lösung auch dann wenn es mehr wie 1 Child gibt?


Gruß
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Wed Feb 27, 2013 9:33 am    Post subject: Reply with quote

Achso stimmt, sorry, du wolltest ja X Child-Elemente haben. Habs ganz vergessen. Mit meiner Lösung funktioniert das ganze natürlich nicht. Schau dir dazu am besten die {call}-funktion an
Back to top
View user's profile Send private message Visit poster's website
eazytrader
Smarty Rookie


Joined: 16 Jul 2010
Posts: 33

PostPosted: Wed Feb 27, 2013 9:56 am    Post subject: Reply with quote

Das hab ich schon getan. Wäre auch die beste Lösung.
Nur leider bekomme ich es hier einfach nicht hin, den Kategorienamen auszulesen.

Ich poste mal das komplette Beispiel. Vielleicht kommst ja du darauf....ich wäre echt sehr dankbar dafür.

Code:

        public function createarray($parent_id = 0){

            $db = Registry::get('pdo');

            $select = $db->prepare("select c.categories_id, c.parent_id, cd.categories_name from :table_categories c left join :table_categories_description cd on c.categories_id = cd.categories_id where parent_id = :parent_id order by sort_order");
            $select->bindValue(':parent_id', $parent_id);
            $select->execute();

            return $select->fetchAll();
        }



        public function listCategory($parent_id = 0, $level = 0){

            $menue = array();

            foreach($this->createarray($parent_id) as $value){
                $menue[] = $this->listCategory($value['categories_id'],$level+1);
            }

            return $menue;

        }


Und dann im Template:

Code:

{function name=menu level=0}
  <ul class="level{$level}">
  {foreach $data as $entry}
    {if is_array($entry)}
      <li>{$entry@key}</li>
       {menu data=$entry level=$level+1}
    {else}
      <li>{$entry}</li>
    {/if}
  {/foreach}
  </ul>
{/function}

{menu data=$menu}


Gruß und Danke
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Wed Feb 27, 2013 10:03 am    Post subject: Reply with quote

Du rufst die call-funktion ja gar nicht auf. musst du nicht:

Code:
{call name=menu data=$entry level=$level+1}


im if-Zweig schreiben?
Back to top
View user's profile Send private message Visit poster's website
eazytrader
Smarty Rookie


Joined: 16 Jul 2010
Posts: 33

PostPosted: Wed Feb 27, 2013 10:19 am    Post subject: Reply with quote

Funktioniert sowohl als auch.
Ich bekomme auch das gewünschte Ergebniss ausgegeben.

Aber ich weis nicht wie ich auf z.b. categories_name zugreifen kann.
Momentan wird nur der Key mit {$entry@key} ausgegeben.

ne Idee?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Feb 27, 2013 10:28 am    Post subject: Reply with quote

Grizzly wrote:
Du rufst die call-funktion ja gar nicht auf. musst du nicht:

Code:
{call name=menu data=$entry level=$level+1}


im if-Zweig schreiben?


{call....} muss nur verwendet werden wenn die template function in einem externem Template definiert ist.

Ansonsten überprüfe mal den Inhalt von $menue mit {$menue|print_r}
Back to top
View user's profile Send private message
eazytrader
Smarty Rookie


Joined: 16 Jul 2010
Posts: 33

PostPosted: Wed Feb 27, 2013 10:48 am    Post subject: Reply with quote

Ich habs jetzt mal so probiert.
Die Daten bekomme ich so auch ins Array.
Den Kategorienamen kann ich im Template so auch ansprechen.

Jedoch ist die Ausgabe vollkommen durcheinander. Die Kategorien werden doppelt und dreifach aufgelistet.

Die Funktion um das Array zu erzeugen:



Code:

        public function listCategory($parent_id = 0, $level = 0){

            $menue = '';

            foreach($this->createarray($parent_id) as $value){

                 $menue[] = array('id' => $value['categories_id'], 'name' => $value['categories_name']);
                 $menue[] = $this->listCategory($value['categories_id'],$level+1);

            }

            return $menue;

        }


Das Array $data sieht so aus:

Code:

Array ( [0] => Array ( [id] => 1 [name] => Hauptkategorie 1 ) [1] => Array ( [0] => Array ( [id] => 7 [name] => Unterkategorie von 1 ) [1] => [2] => Array ( [id] => 8 [name] => Unterkategorie von 1 ) [3] => ) [2] => Array ( [id] => 2 [name] => Hauptkategorie 2 ) [3] => Array ( [0] => Array ( [id] => 9 [name] => Unterkategorie von 2 ) [1] => [2] => Array ( [id] => 10 [name] => Unterkategorie von 2 ) [3] => ) [4] => Array ( [id] => 3 [name] => Hauptkategorie 3 ) [5] => [6] => Array ( [id] => 4 [name] => Hauptkategorie 4 ) [7] => [8] => Array ( [id] => 5 [name] => Hauptkategorie 5 ) [9] => [10] => Array ( [id] => 6 [name] => Hauptkategorie 6 ) [11] => )


Und im Template so:

Code:

{function name=menu level=0}
  <ul class="level{$level}">
  {foreach $data as $entry}
    {if is_array($entry)}
      <li>{$entry.name}</li>
       {menu data=$entry level=$level+1}
    {else}
      <li>{$entry}</li>
    {/if}
  {/foreach}
  </ul>
{/function}

{menu data=$menu}


So schwer hatte ich mir das nicht vorgestellt :-/
Jemand ne Idee an was es liegen könnte?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Feb 27, 2013 12:24 pm    Post subject: Reply with quote

1. Deine Unterkategorien müssen Element des 'id', 'name' Arrays sein.

Code:
        public function listCategory($parent_id = 0, $level = 0){

            $menue = '';

            foreach($this->createarray($parent_id) as $value){

                 $menue[] = array('id' => $value['categories_id'], 'name' => $value['categories_name'], 'subcat' => $this->listCategory($value['categories_id'],$level+1));

            }

            return $menue;

        }


Das Template denke ich so:
Code:
{function name=menu level=0}
  <ul class="level{$level}">
  {foreach $data as $entry}
     <li>{$entry.name}</li>
    {if is_array($entry.subcat)}
       {menu data=$entry.subcat level=$level+1}
    {/if}
  {/foreach}
  </ul>
{/function}

{menu data=$menu}


Der Kode ist jedoch nicht von mir getestet
Back to top
View user's profile Send private message
eazytrader
Smarty Rookie


Joined: 16 Jul 2010
Posts: 33

PostPosted: Wed Feb 27, 2013 12:31 pm    Post subject: Reply with quote

Boahh ....wo soll ich die Kiste Bier hinschicken? Smile
Ich war schon kurz davor alles zu löschen.

Danke Dir, das klappt super!
Und jetzt wo ich es sehe, auch vollkommen logisch.

Gruß
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: German 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