开发者

Display images from a folder on website dynamically. Foreach using Smarty

开发者 https://www.devze.com 2023-03-09 12:27 出处:网络
I want to display random images from a folder on my website example: ../images/ I already thought of using a foreach construction only I开发者_C百科 don\'t know how to implement it in the code.

I want to display random images from a folder on my website example: ../images/ I already thought of using a foreach construction only I开发者_C百科 don't know how to implement it in the code.

<ul>
{foreach from=../images/ item=imagelink}

<li><img src="{$imagelink}" width="100" height="100" alt=""/></li>

{foreach}
</ul>

I hope someone has a solution I already searched a lot but couldn't find a solution.

I also want to show lateron all the images that are in that folder.


You'll probably want to create your list of random images in PHP, then assign the array to a Smarty variable. Then you'll be able to iterate over the array with a foreach and create your list elements.

<?php

// create array of random images into $images
$smarty->assign(images, $images);

?>


{* template *}
<ul>
    {foreach from=$images item=image}
        <li><img src="{$image}"/></li>
    {/foreach}
</ul>


@paul-delre working properly, so I take the url of all the images in that directory use

$files = glob("img/*.*"); 

and

$smarty->assign(images, $files);

Thanks for your help :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号