Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

foreach only displays last item

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Wajpas
Smarty Rookie


Joined: 04 Nov 2020
Posts: 5

PostPosted: Wed Nov 04, 2020 5:09 pm    Post subject: foreach only displays last item Reply with quote

Hey folks,

it's my first project with smarty so be gentle Wink

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
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 278
Location: West Coast

PostPosted: Wed Nov 04, 2020 9:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
Wajpas
Smarty Rookie


Joined: 04 Nov 2020
Posts: 5

PostPosted: Wed Nov 04, 2020 9:58 pm    Post subject: Reply with quote

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 Wink

You see - just for testing i most of the situations use "echo"...
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 278
Location: West Coast

PostPosted: Wed Nov 04, 2020 11:43 pm    Post subject: Reply with quote

"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
View user's profile Send private message
Wajpas
Smarty Rookie


Joined: 04 Nov 2020
Posts: 5

PostPosted: Thu Nov 05, 2020 12:06 am    Post subject: Reply with quote

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 Wink
Back to top
View user's profile Send private message
Wajpas
Smarty Rookie


Joined: 04 Nov 2020
Posts: 5

PostPosted: Thu Nov 05, 2020 4:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 278
Location: West Coast

PostPosted: Mon Nov 09, 2020 7:43 am    Post subject: Reply with quote

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
View user's profile Send private message
Wajpas
Smarty Rookie


Joined: 04 Nov 2020
Posts: 5

PostPosted: Mon Nov 09, 2020 7:38 pm    Post subject: Reply with quote

Thank you verry much...

your code is working how it should!
Helps me verry much!!!

Thank you!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General 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