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

Wert nur einmal anzeigen

 
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
Sven
Smarty n00b


Joined: 04 Feb 2005
Posts: 3

PostPosted: Fri Feb 04, 2005 11:51 am    Post subject: Wert nur einmal anzeigen Reply with quote

Hallo,

ich habe eine Tabelle, in die Werte über eine foreach Schleife gefüllt werden.

Bei meiner SQL-Abfrage bekomme ich aber mehrere Zeilen zurück und möchte aber nur einmal den Wert in die Tabelle schreiben.
Wahrscheinlich ein bißchen unverständlich.

Tabelle:
-------------------------------
-------------------------------
Wert | bla | A_Wert | bla
| bla | |bla
|bla | |bla
---------------------------------
Wert1 |bla | A1_Wert | bla
|bla | | bla

Ich hoffe, daß es so verständlicher ist.

Wert und A_Wert kommen aus dem selben Array.

Hat jemand eine Idee?

Danke
Sven
Back to top
View user's profile Send private message
tonitone
Smarty n00b


Joined: 04 Feb 2005
Posts: 3

PostPosted: Fri Feb 04, 2005 12:38 pm    Post subject: Reply with quote

hast du mehrere eintrage in der datenbank doppelt die du aber nur einmal angezeigt haben willst?
Back to top
View user's profile Send private message
Sven
Smarty n00b


Joined: 04 Feb 2005
Posts: 3

PostPosted: Fri Feb 04, 2005 1:14 pm    Post subject: Reply with quote

Ich bekomme mehrere Zeilen aus der SQL-Abfrage zurück:

wert bla bla a_wert
wert bla bla a_wert
wert bla bla a_wert
wert bla bla a_wert
wert bla bla a_wert
wert bla bla a_wert
wert bla bla a_wert

wert1 bla bla a1_wert
wert1 bla bla a1_wert
wert1 bla bla a1_wert
wert1 bla bla a1_wert
wert1 bla bla a1_wert
wert1 bla bla a1_wert

usw.
Back to top
View user's profile Send private message
tonitone
Smarty n00b


Joined: 04 Feb 2005
Posts: 3

PostPosted: Fri Feb 04, 2005 1:45 pm    Post subject: Reply with quote

also wenn dies ein JA ist, dann wäre die einfachste möglichkeit, gleich dein mysql-statement so abzuschicken das er doppelte ausgaben ausschließt

Code:

'SELECT DISTINCT this,that FROM mytable ORDER BY whatever'


zauberwort ist DISTINCT nach dem SELECT

damit werden keine doppelten einträge aus mysql zurückgegeben

hope it helps
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Feb 04, 2005 2:16 pm    Post subject: Reply with quote

tonitone wrote:
zauberwort ist DISTINCT nach dem SELECT


glaube ich nicht. ich glaube ich denke Sven will schon doppelte bla ausgeben, aber keine doppelten wert.
das kann man lösen, wenn man sich in jedem schleifendurchlauf den vorigen wert merkt.
z.b.:
Code:

{assign var=prev_wert value=null} {* merker initialisieren *}
{foreach from=$table item=item}
  {if $item.wert != $prev_wert}  {* wert nur ausgeben wenn er sich ändert *}
     Wert: {$item.wert}
     {assign var=prev_wert value=$item.wert}  {* previous wert merken *}
  {/if}
  {$item.bla} {* bla immer ausgeben *}
{/foreach}


sowas kann man auch mit mehreren merkern cascadiert machen.

noch besser finde ich allerdings die struktur in php vorzukauen und ein mehrdimensionales array an smarty zu assignen:

[php:1:2812599982]$db->query(...);
$list = array();
while ($row = $db->next_row()) {
if (!isset($list[$row['wert']])) $list[$row['wert']] = array();
$list[$row['wert']][] = $row;
}
print_r($list); // zum testen/veranschaulischen was da rauskommt
$smarty->assign('list', $list);
[/php:1:2812599982]

und im template dann:

Code:

{foreach from=$list key=wert item=subrows}
  {$wert}
  {foreach from=$subrows item=row}
     {$row.bla}
  {/foreach}
{/foreach}


ist wohl z. T. auch geschmacksache.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Sven
Smarty n00b


Joined: 04 Feb 2005
Posts: 3

PostPosted: Fri Feb 04, 2005 2:45 pm    Post subject: Reply with quote

Genau messju,
du hattest Recht, so wollte ich es.
Besten Dank.

Sven
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