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

array ausgeben - foreach?

 
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
rok°!
Smarty Rookie


Joined: 10 Jan 2005
Posts: 12

PostPosted: Wed Feb 09, 2005 8:24 am    Post subject: array ausgeben - foreach? Reply with quote

hallo!

ich habe einen multidimensionalen array:
Code:
   Array (9)
0 => Array (5)
  nr => 2
  name => Visitenkarten / Postkarten
  bild => empty
  beschreibung => Ausführungen in foliert, unfoliert, m...
  sub => 1
1 => Array (5)
  nr => 3
  name => Briefbögen / Wurfzettel
  bild => empty
  beschreibung => Formate in A5 und A4, 1- bis 4-farbig...
  sub => 1
2 => Array (5)
  nr => 4
  name => Poster / Plakate
  bild => empty
  beschreibung => Digital- oder Offsetgedruckt von A2 b...
  sub => 1
[...]


diesen gebe ich mittels foreach-schleife problemlos aus. allerdings immer eine angabe (artikel) pro tabellenzeile. jetzt möchte ich aber 2 artikel pro tabellenzeilen anzeigen. in php würde ich das wohl mit einer for-schleife lösen. nur wie kann ich das in smarty realisieren? hat jemand eine idee?
Back to top
View user's profile Send private message
CaptainE
Smarty Rookie


Joined: 23 Dec 2004
Posts: 22

PostPosted: Thu Feb 10, 2005 11:41 am    Post subject: Reply with quote

also da dein array ja sowiso indiziert ist kannst du anstelle von foreach auch section nehmen.

wenn du dann das atribut step in section auf 2 setzte kannst du das ungefähr so machen:
[php:1:eebb9fb2db]<?php
{section step=2 loop=$array name=test}
// ausgabe es elements $array[test]
{if !empty($array[test + 1]) }
// ausgabe es elements $array[test +1]
{else}
// ausgabe eines leeren feldes
{/if}
{/section}
?>[/php:1:eebb9fb2db]
Back to top
View user's profile Send private message
rok°!
Smarty Rookie


Joined: 10 Jan 2005
Posts: 12

PostPosted: Thu Feb 10, 2005 12:07 pm    Post subject: Reply with quote

ehem, wie soll das denn funktionieren?
$array[test + 1]
--------------^^

das gibts sogar ne schicke fehlermeldung:
"syntax error: unbalanced parenthesis in if statement"
Back to top
View user's profile Send private message
rok°!
Smarty Rookie


Joined: 10 Jan 2005
Posts: 12

PostPosted: Thu Feb 10, 2005 12:19 pm    Post subject: Reply with quote

danke trotzdem. section war das zauberwort. das hat mir weiter geholfen!
so geht's:

[php:1:52e5e7b998]{section step=2 loop=$kat name=kat}
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="250">
{$kat[kat.index].name}
</td>
{if !empty($kat[kat.index_next].name) }
<td width="250">
{$kat[kat.index_next].name}
</td>
</tr>
{else}
<td width="250">
leere zelle
</td>
</tr>
{/if}
</table>
{/section} [/php:1:52e5e7b998]
Back to top
View user's profile Send private message
CaptainE
Smarty Rookie


Joined: 23 Dec 2004
Posts: 22

PostPosted: Thu Feb 10, 2005 3:54 pm    Post subject: Reply with quote

also das mit dem [test + 1] war ja auch nur als hinweis gedacht.

ob das so tut oder nicht hab ich natürlich nicht extra ausprobiert aber es hat ja zur richtigen lösung geführt und somit auch den zweck erfüllt.
Back to top
View user's profile Send private message
rok°!
Smarty Rookie


Joined: 10 Jan 2005
Posts: 12

PostPosted: Sat Feb 26, 2005 7:24 pm    Post subject: Reply with quote

jetzt hab ich schmerzlich feststellen müssen, dass es ja doch nicht geht, da index leider die iteration nicht hochsetzt. index_next gibt zwar den nächsten wert aus, bleibt aber unbeeindruckt von den steps. bzw. das ausgeben der steps beziehts sich auch auf den index_next-befehl.

bsp:
step = 2

produkt1 produkt3
produkt3 produkt5

oder:

step = 1
produkt1 produkt2
produkt2 produkt3

was vöölig gurk ist, denn ich hätte gern:

produkt1 produkt2
produkt3 produkt4
...

weiß vielleicht noch jemand rat? ich öchte nicht auf php im template zurückgreifen müssen.
Back to top
View user's profile Send private message
dan_zm
Smarty Rookie


Joined: 25 Feb 2005
Posts: 6
Location: Zella-Mehlis, Germany

PostPosted: Sun Feb 27, 2005 1:06 pm    Post subject: so gehts Reply with quote

hi,

also erstmal der code

[php:1:ac0c8a539f]<table width="500" border="0" cellspacing="0" cellpadding="0">
{section step=1 loop=$kat name=i}
{if !($smarty.section.i.index%2)}
<tr>
{/if}
<td width="250">
{$kat[i]}
</td>
{if !($smarty.section.i.index_next%2)}
</tr>
{/if}
{/section}
{if $smarty.section.i.loop%2}
<td width="250">
leere Zelle
</td>
</tr>
{/if}
</table>[/php:1:ac0c8a539f]

so funktionierts.
also step ist 1, d.h. er durchläuft alle einträge.
er überprüft nun halt immer, ob der eintrag eine gerade zahl ist oder nicht und setzt dementsprechend die <tr> und </tr> tags.
am ende überprüft er noch, ob die gesamtzahl der elemente gerade oder ungerade ist, und falls sie ungerade ist, hängt er noch ne leere zelle dran.
_________________
The following sentence is a lie. The preceding sentence is true.
Back to top
View user's profile Send private message
rok°!
Smarty Rookie


Joined: 10 Jan 2005
Posts: 12

PostPosted: Sun Feb 27, 2005 1:25 pm    Post subject: Reply with quote

cool. danke! klappt tatsächlich *froi*

in einem anfall fon wahnsinn hab ich das jetzt so hingebofgen, dass man sogar 4 in eine reihe bekommt Very Happy
man mus nur den wert "%2" in "%4" ändern Smile

puh, wochenende gerettet Cool
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