View previous topic :: View next topic |
Author |
Message |
Wajpas Smarty Rookie
Joined: 04 Nov 2020 Posts: 9
|
Posted: Wed Nov 04, 2020 5:09 pm Post subject: foreach only displays last item |
|
|
Hey folks,
it's my first project with smarty so be gentle
My Problem is, that my foreach only displays the last item of an array:
So my PHP-code:
Code: | $smarty->assign('file_arr',$fileinfos['basename']); |
My Array "$fileinfos['basename']" displays with "echo":
aside.tpl
footer.tpl
head_include.tpl
header.tpl
impressum.tpl
index.tpl
layout.tpl
login.tpl
logout.tpl
navi.tpl
register.tpl
tpl_edit.tpl
And then, there is my Template-Code like this:
Code: | {foreach from=$file_arr item=$tpl_link}
<a target="_blank" href="tpl_edit2.php?datei={$tpl_link}">{$tpl_link}</a><br>
{/foreach} |
You see, there should be Links to all of thoses TPL-files, because it leads -if it will work- to an editor for those files.
But in my "tpl_edit.tpl" where there should be an URL-list, there's only one Link - the lastone to "tpl_edit2.php?datei=tpl_edit" .
I hope you're able to help!
Last edited by Wajpas on Wed Nov 04, 2020 11:18 pm; edited 2 times in total |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Wed Nov 04, 2020 9:23 pm Post subject: |
|
|
Just to clarify something:
When you say you used echo to show the contents of $fileinfos['basename'], are you sure?
I mean to say, PHP will usually need to use print_r() to show an array. |
|
Back to top |
|
Wajpas Smarty Rookie
Joined: 04 Nov 2020 Posts: 9
|
Posted: Wed Nov 04, 2020 9:58 pm Post subject: |
|
|
Yes, i know...
Surely print_r displays the same, but inside that foreach loop in php to read out thoese files on the server, which end with ".tpl", echo works as well to display
You see - just for testing i most of the situations use "echo"... |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Wed Nov 04, 2020 11:43 pm Post subject: |
|
|
"inside that foreach loop in php"
I see. I didn't read your post in that way.
I've run a couple of tests and it is working as expected.
Could it be that the page real estate given to show this output is too short to show all but the bottom of the list? |
|
Back to top |
|
Wajpas Smarty Rookie
Joined: 04 Nov 2020 Posts: 9
|
Posted: Thu Nov 05, 2020 12:06 am Post subject: |
|
|
bsmither wrote: |
Could it be that the page real estate given to show this output is too short to show all but the bottom of the list? |
It's an idea, but no - it's just relativ to the text i want to show...
If i write anything long, it's shown with every line i write in that *.tpl...
I think there is something with that foreach...
I'll show you all the php-foreach loop later this day - now i'm not at home so i cannot copy it... Perhaps there's anything in the php-file, because if you say you've tried, i think you just wrote an array by yourself, didn't you?
My array in php get's filled by reading out a direction on my host - perhaps there's anything that fails...
But just thank you for you support until now on this point |
|
Back to top |
|
Wajpas Smarty Rookie
Joined: 04 Nov 2020 Posts: 9
|
Posted: Thu Nov 05, 2020 4:09 pm Post subject: |
|
|
So here it is, the php-script:
Code: |
//Auslesescript
$verzeichnis = "./templates/"; // Auszulesendes Verzeichnis
$sortierung = "0"; // Sortierung ("0"=A-Z, "1"=Z-A)
$extension = "tpl"; // Gewünschte Dateiendung
$dateien = scandir($verzeichnis, $sortierung);
foreach ($dateien as $datei)
{
$fileinfos = pathinfo($verzeichnis."/".$datei);
$filesize = ceil(filesize($verzeichnis."/".$datei)/1024);
if ($datei != "." && $datei != ".." && $fileinfos['basename'])
{
$dateitypen= array("$extension");
if(in_array($fileinfos['extension'],$dateitypen))
{
unset($datei);
echo $fileinfos['basename']."<br>"; //"echo" to display the array ;)
$smarty->assign('file_arr',$fileinfos['basename']);
};
};
}; |
And here 4 remembering the TPL-Code:
Code: | {foreach from=$file_arr item=$tpl_link}
<!-- <a target="_blank" href="tpl_edit2.php?datei=" -->
<a target="_blank" href="tpl_edit2.php?datei={$tpl_link}">{$tpl_link}</a><br>
{/foreach} |
So now I hope for anyone to find my failure.... |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Mon Nov 09, 2020 7:43 am Post subject: |
|
|
You are within a foreach loop, and while in the loop you are assigning each time the basename to 'file_arr'.
When the loop finishes, the last iteration's value of basename will have been assigned to 'file_arr'.
Suggest within the loop:
Code: | $basenames[] = $fileinfos['basename']; |
Then outside the loop:
Code: | $smarty->assign('file_arr',$basenames); // Give collection of basenames to Smarty |
Smarty assign() does not 'stack' values onto the target variable. However, the append() method might suit you (depends on the version of Smarty being used). |
|
Back to top |
|
Wajpas Smarty Rookie
Joined: 04 Nov 2020 Posts: 9
|
Posted: Mon Nov 09, 2020 7:38 pm Post subject: |
|
|
Thank you verry much...
your code is working how it should!
Helps me verry much!!!
Thank you! |
|
Back to top |
|
|