View previous topic :: View next topic |
Author |
Message |
deception Smarty Rookie
Joined: 07 Sep 2004 Posts: 7
|
Posted: Thu Sep 09, 2004 11:24 am Post subject: Probelm mit verschachtelten Schleifen |
|
|
Hallo Board.
PHP Code:
Code: |
$sql = "SELECT * FROM download_folder";
$result = mysql_query($sql);
while ($data = mysql_fetch_array($result)) {
$smarty_data[] = $data;
$sql1 = "SELECT * FROM download_folder, download WHERE download_folder.id=".$data[id]." AND fid=download_folder.id";
$result1 = mysql_query($sql1);
while ($data1 = mysql_fetch_array($result1)) {
$smarty_data1[] = $data1;
}
//$smarty->assign('test', $sql1);
//$smarty->assign('test1', $data);
}
$smarty->assign('downloads_category', $smarty_data);
$smarty->assign('downloads', $smarty_data1);
$smarty->display('main_downloads.tpl');
|
Template Code:
Code: |
{foreach from=$downloads_category item=category}
<p id="normal">{$category.name}</p>
{foreach from=$downloads item=download}
<a href="{$download.path}{$download.filename}">{$download.name}</a><br />
{/foreach}
{/foreach}
|
Ausgabe im Browser:
Code: |
Textverarbeitung
Acrobat Reader
Visio2003 Viewer
Make Tiff
Bildbearbeitung
Acrobat Reader
Visio2003 Viewer
Make Tiff
|
So sieht die Db aus:
Code: |
Feld Typ Null Standard
id int(11) Nein
name tinytext Nein
path tinytext Nein
filename tinytext Nein
fid int(11) Nein 0
|
Code: |
Feld Typ Null Standard
id int(11) Nein
name tinytext Nein
|
Wiso gibt er mir unter Bildbearbeitung und Textverarbeitung immer dasselbe aus? Eigentlich sollte unter Bildbearbeitung nur Make Tiff stehen und der Acrobat und Visio unter Textverarbeitung.
Ich denke es liegt mit der Übergabe der Variable an das .tpl zusammen, komm aber nicht drauf.
Mfg Max |
|
Back to top |
|
MLucas Smarty Rookie
Joined: 09 Sep 2004 Posts: 6 Location: Thuringia, Germany
|
Posted: Thu Sep 09, 2004 1:03 pm Post subject: |
|
|
Probier doch einfach mal folgendes, um das Problem einzugrenzen:
[php:1:165806ab7c]
/* nach dieser zeile*/ $smarty->assign('downloads', $smarty_data1);
print_r($smarty_data);
print_r($smarty_data1);
[/php:1:165806ab7c]
Das zeigt dir dann die Arrays so an, wie sie an smarty übergeben werden.
HTH |
|
Back to top |
|
deception Smarty Rookie
Joined: 07 Sep 2004 Posts: 7
|
Posted: Thu Sep 09, 2004 1:24 pm Post subject: |
|
|
Code: |
Array ( [0] => Array ( [0] => 1 [id] => 1 [1] => Textverarbeitung [name] => Textverarbeitung ) [1] => Array ( [0] => 2 [id] => 2 [1] => Bildbearbeitung [name] => Bildbearbeitung ) ) Array ( [0] => Array ( [0] => 1 [id] => 1 [1] => Textverarbeitung [name] => Acrobat Reader [2] => 1 [3] => Acrobat Reader [4] => ../Downloads/ [path] => ../Downloads/ [5] => AcroReader51_DEU_full.exe [filename] => AcroReader51_DEU_full.exe [6] => 1 [fid] => 1 ) [1] => Array ( [0] => 1 [id] => 2 [1] => Textverarbeitung [name] => Visio2003 Viewer [2] => 2 [3] => Visio2003 Viewer [4] => ../Downloads/ [path] => ../Downloads/ [5] => VVIEWER.EXE [filename] => VVIEWER.EXE [6] => 1 [fid] => 1 ) [2] => Array ( [0] => 2 [id] => 3 [1] => Bildbearbeitung [name] => Make Tiff [2] => 3 [3] => Make Tiff [4] => ../Downloads/ [path] => ../Downloads/ [5] => maketiff.exe [filename] => maketiff.exe [6] => 2 [fid] => 2 ) ) |
Also ich werd nicht schlau draus... |
|
Back to top |
|
MLucas Smarty Rookie
Joined: 09 Sep 2004 Posts: 6 Location: Thuringia, Germany
|
Posted: Thu Sep 09, 2004 2:15 pm Post subject: |
|
|
Aber ich, zumindestens teilweise:
Code: |
Array (
[0] => Array (
[id] => 1
[name] => Textverarbeitung
)
[1] => Array (
[id] => 2
[name] => Bildbearbeitung
)
)
Array (
[0] => Array (
[id] => 1
[name] => Acrobat Reader
[path] => ../Downloads/
[filename] => AcroReader51_DEU_full.exe
[fid] => 1
)
[1] => Array (
[id] => 2
[name] => Visio2003 Viewer
[path] => ../Downloads/
[filename] => VVIEWER.EXE
[fid] => 1
)
[2] => Array (
[id] => 3
[name] => Make Tiff
[path] => ../Downloads/
[filename] => maketiff.exe
[fid] => 2
)
)
|
Wie du siehts, liest er die Kategorien richtig aus und auch die Dateien. Er ordnet auch die Dateien der richtigen Kategorie zu (s. o. nur bei MakeTiff ist [1]=>Bildbearbeitung)
Also stimmt was an deinem Template nicht.
Es könnte z.B. so lauten:
[php:1:233d507e8b]
{section name=kat loop=$downloads_category}
<b>{$download_category[kat].name}:</b><br>
{section name=item loop=$downloads}
{if $downloads[item].fid==$download_category[kat].id}
{$downloads[item].name}<br>
{/if}
{/section}
{/section}
[/php:1:233d507e8b]
Es ist allerdings keine elegante Lösung. PS: Nicht getestet.
BTW: Nutze lieber mysql_fetch_assoc (weil du nur assoziative Arrays nutz anstatt numerischer Arrays) |
|
Back to top |
|
deception Smarty Rookie
Joined: 07 Sep 2004 Posts: 7
|
Posted: Thu Sep 09, 2004 2:18 pm Post subject: |
|
|
Danke ich versuchs mal.
[edit]
Geht leider nicht.
Jetzt gibt er mir nurnoch zwei Doppelpunkte untereinander aus. Wo die wohl herkommen?
[edit 2]
Die Doppelpunkte kamen von nem Vertippsler.
Jetzt gibt er garnichtsmehr aus! |
|
Back to top |
|
MLucas Smarty Rookie
Joined: 09 Sep 2004 Posts: 6 Location: Thuringia, Germany
|
Posted: Thu Sep 09, 2004 2:25 pm Post subject: |
|
|
Hab mich auch vertippt:
Code: |
{section name=kat loop=$downloads_category}
<b>{$downloads_category[kat].name}:</b><br>
{section name=item loop=$downloads}
{if $downloads[item].fid==$downloads_category[kat].id}
{$downloads[item].name}<br>
{/if}
{/section}
{/section}
|
Jetzt aber |
|
Back to top |
|
deception Smarty Rookie
Joined: 07 Sep 2004 Posts: 7
|
Posted: Thu Sep 09, 2004 2:33 pm Post subject: |
|
|
Juhu Danke!
Noch was, bissle Kleineres:
Code: |
{foreach from=$telefon item=tele}
<tr>
<td id="td-name"><input name="name{$i}" type="text" size="10" maxlength="30" value={$tele.name}></td>
{/foreach}
|
Code: |
while ($data = mysql_fetch_array($result)) {
$smarty_data[] = $data;
$smarty->assign('i', $data[id]);
$smarty->assign('telefon', $smarty_data);
}
|
Oben soll {$i} mit einer fortlaufenden Zahl ausgefüllt werden (mit der id aus der db).
Er gibt mir aber in jedem Satz eine 2 aus. Wiso? liegt das an der Übergabe der Variable? |
|
Back to top |
|
MLucas Smarty Rookie
Joined: 09 Sep 2004 Posts: 6 Location: Thuringia, Germany
|
Posted: Thu Sep 09, 2004 2:41 pm Post subject: |
|
|
Betrachtet man sich die while Schleife, sieht man, dass i jedesmal neu gesetzt wird, im letzten Durchgang ist $data[id]=2 und deswegen auch i=2.
Folgendes sollte funktionieren:
[php:1:3e78abf0c5]
$smarty_data = array();
while ($data = mysql_fetch_assoc($result))
{
$smarty_data[] = $data;
}
$smarty->assign("telefon",$smarty_data);
[/php:1:3e78abf0c5]
Code: |
{section name=tel loop=$telefon}
<tr>
<td id="td-name"><input name="name{$telefon[tel].id}" type="text" size="10" maxlength="30" value={$telefon[tel].name}></td>
</tr>
{/section}
|
|
|
Back to top |
|
deception Smarty Rookie
Joined: 07 Sep 2004 Posts: 7
|
Posted: Thu Sep 09, 2004 2:42 pm Post subject: |
|
|
Hab im Kreis Gedacht. Habe einfach tele.id genommen und dan ging es. aber das andere hätte doch auch funktionieren sollen, oder kann ich innerhalb einer foreach schleife nicht auf andere Variablen auser die in der Anweisung übergebenen zugreifen? |
|
Back to top |
|
|